PHP supports the following data types
- String
- Float(Double)
- Integer
- Boolean
- Array
- NULL
- Resource
- Object
Echo “This is nice”; // this is an example of string
Echo 1; // this is an example of integer
Echo -1; // this is also an integer
Echo 1.25 // this is an example of double
?>
- The string will always be wrapped in quotes.
- The numeric value(positive or negative) without decimal is an integer.
- The numeric value with decimal is double.
- The other data types will be discussed in the book later.
How to check if variable value is integer, double or Boolean
You can use the following
Var_dump($variable_name);
CODE
$var = 12.25;
var_dump($var);
?>
OUTPUT
$var = “12.25”;
var_dump($var);
?>
OUTPUT
To compare if variable is integer, use is_int
$var = 12;
if(is_int($var))
{
echo “Variable is integer”;
}
?>
OUTPUT
To compare if variable is float, use is_float
$var = 12.25;if(is_float($var))
{
echo “Variable is float”;
}
?>
OUTPUT
To compare if variable is string, use is_string
$var = “12.25”;if(is_string ($var))
{
echo “Variable is string”;
}
?>
OUTPUT
To compare if variable is array, use is_array
$var = array(“12.25”, “Ok”);if(is_array ($var))
{
echo “Variable is array”;
}
?>
OUTPUT
To compare if variable is object, use is_object
$object = new stdClass();
$object->fruits = array(“Apple”, “Banana”, “Mango”, “Papaya”);
if(is_object($object))
{
echo “Variable is object”;
}
?>
OUTPUT
Leave a comment