Types in PHP
| Scalar types | |
| What null means | |
| Type juggling | |
| Inspect types with var_dump |
Scalar types
Four scalar types cover single values: int for whole numbers, float for fractions, string for text, and bool for true and false.
$count = 42; // int $price = 9.5; // float $title = "PHP"; // string $ready = true; // bool
What null means
null means no value at all. A variable holds null when nothing was ever stored in it, and passing null often means the argument was skipped:
$nickname = null; var_dump($nickname); // NULL
Type juggling
PHP converts types on the fly when the operation demands it. A numeric string plus an int gives an int, which surprises newcomers from stricter languages:
var_dump("5" + 3); // int(8) var_dump("5" . 3); // string(2) "53"
The plus sign calculates, while the dot glues. Same values, different operators, different types out.
Inspect types with var_dump
When a value confuses you, dump it. var_dump prints the type, the length, and the value in one call:
var_dump(42); // int(42) var_dump("42"); // string(2) "42" var_dump(null); // NULL
Next: Strings in PHP
Article author: Arthur Isaev