Though PHP is a loosely typed language where type comparison occurs at runtime, It is often required to examine the type of a variable before further processing it. Consider a scenario where you want get the age of a user in a text box and store it in the database, you might want to check if the age entered is in integers or not, in such cases variable examination functions come handy in PHP. Following are some of the most commonly used variable examination functions.
is_int($value)
Checks if the value passed to it is an integer or not.
is_numeric($value)
Checks if the value passed to it is numeric string or a number.
is_float($value)
Checks if the value passed to it is a float
is_string($value)
Checks if the value passed to it is a string or not.
is_bool($value)
Checks if the value passed to it is a boolean value or not
is_array($value)
Checks if the value passed to it is an array or not.
is_empty($value)
Checks if the passed variable contains any value or not. Returns true if variable is empty
unset($value)
Used to delete the value contained by a variable.
Lets have a look at a working examples of variable examination functions.
"; } $val = 10.654; if(is_float($val)) { echo "Variable is a float"; } $val = "68349"; if(is_numeric($val)) { echo "Variable is a numeric."; } $val = "Hello to knowledgehills."; if(is_string($val)) { echo "Variable is string."; } $val = true; if(is_bool($val)) { echo "Variable is a boolean."; } $val = array(34, 54,32,64,12,45); if(is_array($val)) { echo "Variable is an array."; } if(!empty($val)) { echo "Variable is not empty."; } unset($val); // Removing value of $val variable if(empty($val)) { echo "Variable is empty."; } ?>