How to check the data type of a variable in PHP?
Use var_dump() to see the type and value, gettype() to get the type name, or the is_ functions (like is_int(), is_string(), is_array()) to test a type.
<?php
$a = 343;
$b = "Hello";
$c = 3.14;
$d = [1, 2];
var_dump($a);
echo gettype($b) . "<br>";
var_dump(is_float($c));
echo gettype($d);
?>
Output:
Tutorials Class - Output Window
int(343)
string
bool(true)
array
Note:
For a float, gettype() returns the word “double” (an old name). From PHP 8 you can use get_debug_type(), which returns “float”.
In PHP you do not write the data type when creating a variable, PHP decides it from the value. Learn more in PHP Data Types.
Learn more about the similar topics:
| Tutorials |
|---|
| PHP Variables |