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

Scalar types

In the previous lesson, numbers and strings went into the jars. Now let's see how one value differs from another.

Imagine that every jar has a sticker saying what kind of thing is inside: "whole number", "fraction", "text". Nobody sticks it on in advance: PHP changes the sticker itself when you put a new value into the jar. This kind of value is called its type. A type belongs to the value, not to the variable, so one variable can hold a number now and text a line later:

PHP
<?php

$x = 12 / 4;
var_dump($x); // int(3)
$x = 9 / 2;
var_dump($x); // float(4.5)
$x = 'nine';
var_dump($x); // string(4) "nine"

Let's go through it line by line:

  1. 12 / 4 divides evenly, and the whole number 3 goes into $x.
  2. var_dump($x) prints the type and the value: int(3).
  3. 9 / 2 does not divide evenly, and the fraction 4.5 goes into $x—the sticker changes to float.
  4. $x = 'nine' puts a string into the same variable, and var_dump prints string(4) "nine".

When both numbers are whole, the / operator returns a whole number if the division comes out even, and a fraction if something is left over. That is why one and the same operation gave two different types.

The sticker is a simplification here. PHP keeps the type together with the value itself, not on the jar. So with $b = $a, the type moves into $b along with the copy of the value.

Four scalar types and null

A scalar type holds one simple value: a number, a string, or a yes-or-no answer. PHP has four such types, plus the special type null.

int

Whole numbers with no fractional part: 42, -7, 0. They hold item counts, order numbers and ages.

float

Fractional numbers: 3.5, -0.25. Such a number is called a floating-point number. In code, a dot separates the fractional part, not a comma. The number 2.0 is a float too, even though its fractional part is zero.

string

Text in quotes: 'Anna', '42'. The string '42' is text of two characters, not a number.

bool

A boolean value: true or false, "yes" or "no". It answers questions like "has the order been paid".

null

The single value null means "the jar holds nothing". A variable set to null still exists: unlike a variable that was never assigned, it can be read without a warning.

How to read var_dump

The var_dump function prints the type and the value, each value on a line of its own:

PHP
var_dump(42); // int(42)
var_dump(2.0); // float(2)
var_dump('abcd'); // string(4) "abcd"
var_dump(false); // bool(false)
var_dump(null); // NULL

What each line of output means:

  • int(42) is the whole number 42;
  • float(2) is a floating-point number: var_dump does not print a zero fractional part, but it names the type;
  • string(4) "abcd" is a string: its length in parentheses, the string itself in quotes;
  • bool(false) is a boolean value;
  • NULL is the value null, with no parentheses.

You can pass several values to var_dump, separated by commas. It prints each of them on a line of its own, in the same format.

There is also the gettype function, which returns the name of a type as a string. But its names are old: gettype(3.5) gives 'double', and gettype(42) gives 'integer'. That is why this course shows types with var_dump.

Fractions are not exact

A floating-point number is stored in binary, and 0.1 cannot be written in it exactly. In the same way, one third cannot be written exactly in decimal: 0.333… has to be cut off somewhere.

PHP
var_dump(0.1 + 0.2); // float(0.30000000000000004)
echo 0.1 + 0.2, "\n"; // 0.3

var_dump shows the number in full, and you can see the sum is a little more than 0.3. echo prints a fraction to 14 significant digits and rounds it to 0.3, so the error is hidden.

This is a classic interview question: "Why is 0.1 + 0.2 not equal to 0.3?" The answer: a float holds a binary approximation, not an exact decimal number. That is why money is counted in cents and stored as an int.

Common mistake

The fractional part was separated with a comma, as many languages do in ordinary text:

PHP
$price = 3,5; // ✗
TEXT
Parse error: syntax error, unexpected token ","

This is a parse error: PHP does not understand what the comma after 3 is doing, and the file never runs. In code, a dot always separates the fractional part: $price = 3.5;.

Key points

  • A type belongs to the value, not to the variable: a new value brings its own type.
  • There are four scalar types—int, float, string, bool—plus the special type null.
  • var_dump prints the type and the value: int(3), float(4.5), string(4) "abcd", bool(true), NULL.
  • / on two ints gives an int when the division is even, otherwise a float; 0.1 + 0.2 is not exactly 0.3.