Type conversions
In the previous lesson, every value got a sticker with its type. A real jar never changes what it holds: the jam in it does not turn into juice by itself. PHP does: when an operation needs another type, PHP automatically converts the value it took from the jar, and the jar keeps what it held.
Let's see what happens when you add a string to a number:
<?php
$text = '5';
$sum = $text + 3;
var_dump($text); // string(1) "5"
var_dump($sum); // int(8)Let's go through it line by line:
$textholds the string'5'.- Addition needs numbers, so PHP turns the string
'5'into the number5, and8goes into$sum. var_dump($text)shows that the variable$textitself did not change: it still holds a string.var_dump($sum)printsint(8): the result of the addition is a number.
This automatic conversion is called type juggling. PHP converts a value to the type the operation needs and leaves the variable it took the value from untouched.
Explicit casts
Sometimes you need to change the type yourself. To do that, write the type you want in parentheses before the value—this is a type cast:
var_dump((int) 3.9); // int(3)
var_dump((int) '12 apples'); // int(12)
var_dump((int) 'apples'); // int(0)
var_dump(intval('42')); // int(42)
var_dump((float) '3.5'); // float(3.5)
var_dump((string) 42); // string(2) "42"
var_dump((string) true); // string(1) "1"
var_dump((string) false); // string(0) ""
var_dump((string) null); // string(0) ""
var_dump((bool) 0); // bool(false)
var_dump((bool) 'abc'); // bool(true)Let's take each cast in turn.
(int) and intval
A cast to a whole number works like this:
- a fraction loses its fractional part, with no rounding:
(int) 3.9is3; - from a string, the digits at its start are taken:
(int) '12 apples'is12; - a string with no digits at the start gives
0; - the
intvalfunction does the same:intval('42')is42.
A cast reads a string with no error or warning. Whatever it cannot read, it drops silently.
(float)
Reads a fraction from a string: (float) '3.5' is 3.5. The rules for strings are the same as for (int), except that the fractional part is kept.
(string)
Turns a value into text:
- the number
42becomes the string'42'; truebecomes the string'1';falseandnullbecome the empty string''.
(bool)
Gives a boolean value: (bool) 0 is false, and (bool) 'abc' is true.
A string that is not a number
In the first example, the string '5' was a number from start to end. What if the string has letters in it?
echo '12 apples' + 1, "\n"; // ✗
echo 'apples' + 1, "\n"; // ✗A number at the start of the string
The first line runs: PHP takes 12, adds 1 and prints 13. But first it gives a warning, because '12 apples' is not a number as a whole:
Warning: A non-numeric value encounteredA string with no number
In the second line, 'apples' has no number at all. PHP refuses to add such a string to a number and stops the script at this line:
Fatal error: Uncaught TypeError: Unsupported operand types: string + intThis shows the difference between a cast and type juggling. (int) 'apples' silently gives 0, while 'apples' + 1 is a TypeError. Interviewers like to ask what '5' + 3 and 'apples' + 1 give: the answer is 8 and a TypeError.
What echo prints
Before printing, echo turns every value into a string—by the same rules as (string):
- numbers print as digits:
7 / 2prints as3.5, and9.0as9; trueprints as1;falseandnullbecome the empty string, and nothing appears on the screen.
Common mistake
A boolean value was printed with echo:
$isPaid = false;
echo 'paid: ', $isPaid, "\n"; // ✗The program prints paid: and nothing more—no error, no warning. The value false turned into the empty string, so you cannot see it on the screen. To see a boolean value, print it with var_dump: var_dump($isPaid); outputs bool(false).
Key points
- When an operation needs another type, PHP converts the value automatically, and the variable does not change.
- The casts
(int),(float),(string),(bool)read a string silently:(int) '12 apples'is12. - In arithmetic, a string with a number at its start gives a
Warning, and a string with no number gives aTypeError. echoprintstrueas1, andfalseandnullas the empty string.