Detecting the data type of a PHP variable

Detecting the datatype of a specific PHP variable can be done by using PHP gettype() function. refer below for example.

<?php  
 

//define variables  
$on = true;
$age = 20;
$name = "daniel";
$price = 99.99;
 
//Returns Boolean  
echo gettype($on);
 
//Returns String  
echo gettype($name);
 
//Returns Integer  
echo gettype($age);
 
//Returns Double  
echo gettype($price);
 
?>  

You can also use other PHP functions to determine the datatypes of a variable
FUNCTION NAME
is_bool() Checks if a variable is a BOOLEAN
is_string() Checks if a variable is a STRING
is_numeric() Checks if a variable is a NUMERIC STRING
is_int() Checks if a variable is an INTEGER
is_array() Checks if a variable is an ARRAY
is_object() Checks if a variable is an OBJECT
is_null() Checks if a variable is NULL
is_float() Checks if a variable is a FLOAT

Comments