Gettype and Settype Function in PHP

Introduction

In this article I explain the settype() and gettype() functions in PHP. The gettype() function gets the type of variable; gettype() is a function that display a data type. The settype function sets the type of variable; the settype() function changes the data type.

Syntax

gettype($var_name);

 

Parameter  
Var name Pass the variable name

Return Value

Boolean, Integer, Double, String, Array, Object, Resource, NULL

Example1

<?php

$a=100;

echo gettype($a);

?>

Output

 gettype in php.jpg

With Condition

 

In this example I am using "if" and "else" conditions.

 

Example2

 

<?php

$a=5;

if(is_int($a))

echo 'integer';

else

echo 'not integer';

?>

Output

gettype in php.jpg 

Next, I will explain the settype() function in PHP. Such as:

 

Syntax

 

bool settype($var_name);

 

Example

 

<?php

$a=5.2;

echo gettype($a);

echo "<br>";

settype($a,"int");

echo gettype($a);

?>

Output

Settype in PHP.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all