PHP Data Types
PHP supports several data types for storing different kinds of values: String, Integer, Float (double), Boolean, Array, Object, NULL, and a couple of special types used internally. Understanding these helps you predict how PHP will behave with your data.
Because PHP is loosely typed, the same variable can hold different types over its lifetime, and PHP automatically converts between types when needed, a process called type juggling.
gettype($variable);
var_dump($variable);Common scalar types
String holds text like "Hello". Integer holds whole numbers like 25. Float holds decimal numbers like 9.99. Boolean holds true or false.
Checking a variable's type
The gettype() function returns a variable's current type as a string, and var_dump() shows both the type and the value, which is very useful for debugging.
<?php
$a = "Hello";
$b = 25;
$c = 9.99;
$d = true;
echo gettype($a) . " " . gettype($b) . " " . gettype($c) . " " . gettype($d);
?>string integer double booleangettype() reports the current data type of each variable.
<?php
$x = 10;
var_dump($x);
?>int(10)var_dump() prints both the type and value, which is helpful when debugging.
Key points
- PHP's main scalar types are String, Integer, Float and Boolean.
- Arrays, Objects and NULL are also valid PHP data types.
- gettype() returns the type of a variable as a string.
- PHP automatically converts between types when needed (type juggling).
