PHP has eight data types. You'll use most of them constantly; a couple (object, callable) get their own dedicated lessons later in this section.
| Type | Holds | Example |
|---|---|---|
| string | Text | "Hello" |
| int | Whole numbers | 42 |
| float | Decimal numbers | 3.14 |
| bool | true or false | true |
| array | An ordered collection of values | [1, 2, 3] |
| object | An instance of a class | new Person() |
| null | No value at all | null |
| callable | A reference to a function | strtoupper(...) |
gettype() tells you what type a variable currently holds — useful while learning, and occasionally in real debugging:
<?php
$x = 42;
echo gettype($x); // "integer"
?>PHP automatically converts between types when an operation needs it to — this is called type juggling. Add a string that looks like a number to an actual number, and PHP converts the string for you:
<?php
$result = "5" + 3; // 8 (the string "5" is converted to an int)
echo $result;
?>This is convenient, but it can also produce surprising results if you're not paying attention — "5 apples" + 3 throws a warning in modern PHP rather than silently guessing. The next lesson, Type Casting, covers how to convert between types deliberately, instead of leaving it to PHP's automatic rules.
null is its own thing
null is not the same as an empty string "", and it isn't the same as 0 or false either — even though PHP will treat all of them as "falsy" in certain comparisons. null specifically means no value was ever set. This distinction matters more once you start working with databases, where a missing value and an empty one mean genuinely different things.