How to Get Type of Variable in PHP
Type checking will tell us what type of "thing" a variable is. In PHP we can get the variable data type using the built-in
gettype() Pass the variable as the first argument. Here is an example:
$thing = ['a', 'b', 'c']; $result = gettype($thing); echo($result);
The variable we supplied is an
array and that is indeed what
gettype() returned it as.
Here is the full list of possible data type strings
gettype() will return:
- "double" (for historical reasons "double" is returned in case of a float, and not simply "float")
- "resource (closed)" as of PHP 7.2.0
- "unknown type"