Types in PHP

Scalar types

Four scalar types cover single values in PHP: int for whole numbers, float for fractions, string for text, and bool for true and false.

<?php $count = 42; // int $price = 9.5; // float $title = "PHP"; // string $ready = true; // bool var_dump($count, $price, $title, $ready);

php scalars.php

int(42)
float(9.5)
string(3) "PHP"
bool(true)

var_dump() prints one value per line, each with its type in front, so the four types of the four variables are visible at a glance.

What null means

null means no value at all. A variable holds null when you assign null to it, and a parameter can declare null as its default:

<?php $nickname = null; var_dump($nickname); // NULL

php null.php

NULL

An undefined variable is not null: PHP 8.5 warns Warning: Undefined variable $nickname and only then treats the value as null. Assigning null is the honest way to say that a value is missing on purpose, and $nickname === null is the check that asks for it.

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:

<?php var_dump("5" + 3); // int(8) var_dump("5" . 3); // string(2) "53"

php juggling.php

int(8)
string(2) "53"

The plus sign calculates, while the dot glues. Same values, different operators, different types out.

Since PHP 8 a string with no number in it is no longer quietly zero: "apples" + 3 stops with TypeError: Unsupported operand types: string + int, and "5 apples" + 3 keeps the leading number, prints Warning: A non-numeric value encountered and returns 8.

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:

<?php var_dump(42); // int(42) var_dump("42"); // string(2) "42" var_dump(null); // NULL

php inspect.php

int(42)
string(2) "42"
NULL

The two 42 values look the same in echo and behave differently in an operation, which is why the dump is the first tool of every debugging session: int(42) + 1 is 43, while "42" + 1 is 43 only because PHP converts the string first.

Exercises

  1. Dump every variable of scalars.php on its own line with one var_dump() call and compare the four types.
  2. Add $nickname = ""; before the dump in null.php. Does the output change?
  3. Replace the plus sign in juggling.php with a dot and predict the output before you run it.
  4. Run var_dump("apples" + 3); and read the error. Which type does PHP 8.5 refuse to add?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. Which four types are the scalar types of PHP?

    Answer

    int, float, string and bool

    The four scalar types hold one value each: an integer, a floating-point number, a string or a boolean.

  2. How does a variable get the value null?

    Answer

    You assign null to it, or a parameter declares null as its default

    In PHP 8 an undefined variable is a warning, not a quiet null, so null comes from an explicit assignment or a default value.

  3. What does var_dump() print that echo does not?

    Answer

    The type of the value, and the length of a string

    The dump starts with the type, as in string(3) "PHP", which is exactly what echo leaves out.

  4. What does the dot operator do with two values?

    Answer

    Joins them as strings

    The dot is concatenation, so "5" . 3 makes the string "53" and not the number 8.

  5. What does this program print? <?php var_dump("10" + 5);

    Answer

    int(15)

    The plus sign converts the numeric string to an integer, adds 5, and the result is an int.

  6. What does this program print? <?php var_dump("10" . 5);

    Answer

    string(3) "105"

    The dot joins the string and the integer into one string of three characters.

  7. What does this program print? <?php var_dump(1.0);

    Answer

    float(1)

    A number written with a decimal point is a float even when the fraction is zero, and var_dump prints float(1).

  8. What does this program print? <?php $ok = true; var_dump($ok);

    Answer

    bool(true)

    The variable holds a boolean, so the dump prints bool(true) and not the number 1.

  9. What does this program print? <?php $x = null; var_dump($x);

    Answer

    NULL

    The type null has one value, and var_dump prints it as NULL, with no length because there is no value to measure.

  10. A script adds a string with no number in it, such as "apples", to an integer on PHP 8.5. What happens?

    Answer

    PHP stops with a TypeError, because the string has no number to convert

    PHP 8 turns a string without a number into a TypeError instead of the quiet zero of older versions.

The code on this page was run with PHP 8.5.13 (php -v) in Alpine Linux, with the commands shown above.

Article author: Arthur Isaev