Every value has a type
A type says what kind of value something is, and therefore what you can do with it. You can multiply two numbers, but not two sentences; you can make text uppercase, but not a number. PHP has these main types:
| Type | Example | Used for |
|---|---|---|
string |
"Mechanical Keyboard" |
Text of any length |
int |
60 |
Whole numbers |
float |
89.50 |
Numbers with a fractional part |
bool |
true, false |
Yes/no answers |
null |
null |
"No value" |
array |
[1, 2, 3] |
Lists and key-value collections |
object |
new DateTime() |
Values built from classes |
Strings, integers, floats and booleans are called scalar types because they hold a single value. Arrays and objects get their own sections later in the course.
Inspecting values with var_dump
echo prints a value but hides its type. var_dump() shows both, which makes it the first thing to reach for when code behaves oddly:
<?php
var_dump("Mechanical Keyboard");
var_dump(60);
var_dump(89.50);
var_dump(true);
var_dump(null);
var_dump([10, 20]);string(19) "Mechanical Keyboard"
int(60)
float(89.5)
bool(true)
NULL
array(2) {
[0]=>
int(10)
[1]=>
int(20)
}For a string, the number in brackets is its length in bytes. For an array, it is the number of elements, followed by each key and value. gettype() returns just the type name as text, and functions such as is_int(), is_string() and is_bool() answer yes or no:
<?php
$stock = 60;
echo gettype($stock) . "\n";
var_dump(is_int($stock));
var_dump(is_string($stock));
var_dump(is_numeric("12.5"));integer
bool(true)
bool(false)
bool(true)is_numeric() is handy for form input: it is true for a number or a string that looks like one.
Integers and floats
An int is a whole number, positive or negative. A float (also called a double) has a decimal point or an exponent. Dividing two integers gives a float unless the division is exact:
<?php
var_dump(10 / 2);
var_dump(10 / 4);
var_dump(7 + 1.0);
var_dump(PHP_INT_MAX);int(5)
float(2.5)
float(8)
int(9223372036854775807)Integers have a maximum, PHP_INT_MAX, which is 9223372036854775807 on 64-bit systems. Going past it turns the result into a float. The numbers lesson covers rounding and the precision limits of floats.
Booleans and truthiness
A bool is true or false. Conditions in if statements need a boolean, and when you give them something else PHP converts it. Knowing which values count as false saves a lot of confusion. These are the only values that become false:
<?php
var_dump((bool) false);
var_dump((bool) 0);
var_dump((bool) 0.0);
var_dump((bool) "");
var_dump((bool) "0");
var_dump((bool) null);
var_dump((bool) []);bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)
bool(false)Everything else is true, including some values that look false:
<?php
var_dump((bool) "false");
var_dump((bool) " ");
var_dump((bool) "0.0");
var_dump((bool) -1);bool(true)
bool(true)
bool(true)
bool(true)The string "0" is false but "0.0" and "false" are true. This matters when you check a form field: a user who types 0 sends the string "0", which a plain if ($quantity) would treat as "nothing entered".
Converting between types
A cast converts a value on purpose. Write the target type in brackets before the value:
<?php
var_dump((int) "42");
var_dump((int) "42 apples");
var_dump((int) "apples");
var_dump((int) 9.99);
var_dump((float) "3.5");
var_dump((string) 120);
var_dump((bool) "yes");int(42)
int(42)
int(0)
int(9)
float(3.5)
string(3) "120"
bool(true)Two results deserve attention. (int) 9.99 gives 9: casting cuts off the decimals, it does not round. And (int) "apples" gives 0 with no warning, so a cast alone is not validation. To check that input really is a number, use is_numeric() or filter_var() (shown in Validating and escaping form input) before converting it.
The functions intval(), floatval(), strval() and boolval() do the same as the casts and read more naturally in some code.
null
null is the value of "nothing": a variable that was set to null, a function that returns nothing, a missing optional value. is_null($x) and $x === null test for it. The ternary and null coalescing lesson shows the operators that make working with null pleasant.
Common mistakes
- Using echo to debug.
echo false;prints nothing andecho null;prints nothing, so you cannot tell them apart. Usevar_dump(). - Treating "0" as "no value". The string
"0"is false in a condition. Useisset()or compare with''when you mean "empty". - Casting instead of validating.
(int) "abc"silently becomes 0. - Expecting a cast to round.
(int) 2.9is 2. Useround()first if you want 3.
Exercise
Predict the type and value that var_dump() shows for each of these, then run them to check: "15" + 5, "15" . 5, (int) "7.8", (bool) "0" and 10 / 3.
Show one possible answer
<?php
var_dump("15" + 5);
var_dump("15" . 5);
var_dump((int) "7.8");
var_dump((bool) "0");
var_dump(10 / 3);int(20)
string(3) "155"
int(7)
bool(false)
float(3.3333333333333335)+ is maths, so the numeric string is converted to a number. . joins strings, so 5 becomes "5". The cast cuts off .8. "0" is one of the false values. 10 / 3 is not exact, so it is a float.
Key points
- The scalar types are string, int, float and bool. null means no value. Arrays and objects hold several values.
- var_dump() shows a value's type and contents; it is the most useful debugging tool in PHP.
- Casts such as (int) and (string) convert a value on purpose; intval(), floatval() and boolval() do the same.
- Only a few values are false when converted to bool: false, 0, 0.0, '', '0', null and an empty array.