Skip to content
Theory · Variables and types · lesson 4 of 6

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
<?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:

  1. $text holds the string '5'.
  2. Addition needs numbers, so PHP turns the string '5' into the number 5, and 8 goes into $sum.
  3. var_dump($text) shows that the variable $text itself did not change: it still holds a string.
  4. var_dump($sum) prints int(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:

PHP
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.9 is 3;
  • from a string, the digits at its start are taken: (int) '12 apples' is 12;
  • a string with no digits at the start gives 0;
  • the intval function does the same: intval('42') is 42.

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 42 becomes the string '42';
  • true becomes the string '1';
  • false and null become 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?

PHP
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:

TEXT
Warning: A non-numeric value encountered

A 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:

TEXT
Fatal error: Uncaught TypeError: Unsupported operand types: string + int

This 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 / 2 prints as 3.5, and 9.0 as 9;
  • true prints as 1;
  • false and null become the empty string, and nothing appears on the screen.

Common mistake

A boolean value was printed with echo:

PHP
$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' is 12.
  • In arithmetic, a string with a number at its start gives a Warning, and a string with no number gives a TypeError.
  • echo prints true as 1, and false and null as the empty string.