PHP supports eight primitive data types. You don't have to memorize them all at once, but knowing they exist will help you understand error messages and debug your code later.
The four scalar types hold a single value: string, int, float, and bool. The two compound types hold multiple values: array and object. And the two special types: NULL (no value) and resource (external resource like a database connection).
Strings, Integers, Floats, and Booleans
These are the ones you'll use every single day. A string is text inside quotes. An integer is a whole number. A float is a decimal number. A boolean is true or false.
<?php
$greeting = "Hello PHP"; // string
$count = 42; // integer
$price = 9.99; // float
$is_logged_in = false; // boolean
echo $greeting;
?>
Try it Yourself โ
Checking Types with var_dump() and gettype()
PHP gives you two handy functions to inspect variables. var_dump() shows you the type and value. gettype() returns the type as a string. These are invaluable for debugging.
<?php
$name = "Bilal";
$age = 25;
$price = 19.99;
var_dump($name);
var_dump($age);
var_dump($price);
echo gettype($name);
?>
var_dump($name) will output something like string(5) "Bilal", telling you it's a string of 5 characters. gettype($name) just returns the string "string".
Type Juggling and Casting
PHP automatically converts types when needed, a feature called type juggling. If you add a string and a number, PHP converts the string to a number. You can also force a type using casting:
<?php
$result = "10" + 5; // 15 โ PHP converts "10" to 10
$explicit = (int) "10"; // 10 โ explicit cast to integer
$float_val = (float) "3.14"; // 3.14 โ cast to float
var_dump($result);
var_dump($explicit);
var_dump($float_val);
?>