Every value stored in a PHP variable belongs to a specific data type that dictates how PHP stores it and which operations are valid. PHP groups its types into four scalar types, compound types like arrays and objects, and special types like null and resource.
Knowing what type a variable holds prevents subtle comparison and math bugs.
| Category | Type | Example Value | Purpose |
|---|---|---|---|
| Scalar | string |
"Hello PHP" |
Text sequences |
| Scalar | int |
42 |
Whole numbers |
| Scalar | float |
19.99 |
Decimal numbers |
| Scalar | bool |
true / false |
Logical flags |
| Compound | array |
['a', 'b'] |
Ordered maps and lists |
| Compound | object |
new DateTime() |
Class instances |
| Special | null |
null |
Intentional empty value |
Use var_dump() or get_debug_type() during debugging to inspect both the exact type and value of any variable.
<?php
declare(strict_types=1);
$title = "PHP Guide";
$lessonNumber = 7;
$rating = 4.9;
$published = true;
$tags = ["php", "backend", "web"];
$deletedAt = null;
var_dump($title, $lessonNumber, $rating, $published, $tags, $deletedAt);
echo "Detected type: " . get_debug_type($tags) . PHP_EOL;
By default, PHP automatically converts compatible types when needed—for instance, turning "10" into 10 during addition. Adding declare(strict_types=1); at the top of your file stops silent type conversions on function calls.
<?php
declare(strict_types=1);
function addTax(float $price, float $rate): float
{
return $price * (1 + $rate);
}
echo addTax(100.0, 0.15) . PHP_EOL;
empty() with null checks: empty(0) and empty("0") return true. Use $value === null or is_null() when checking specifically for null.=== without rounding: Binary floating-point math can produce 0.30000000000000004. Store money as integer cents or use round().gettype() instead of get_debug_type(): gettype() returns legacy names like "double" instead of "float"; get_debug_type() returns modern PHP 8 type names.NULL and an empty string ""?null represents the absence of any value, whereas "" is a valid string of length zero.
Yes. PHP is dynamically typed, so assigning 42 to $x and later "forty-two" to $x is valid, unless constrained by a typed class property.
Use filter_var($input, FILTER_VALIDATE_INT) !== false rather than is_int(), because HTML form inputs always arrive as strings.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Experiment with the code from this lesson in our interactive playground.