Scalar Types: bool, int, float and string

2.Scalar Types: bool, int, float and string

M

In this chapter

We'll look closely at PHP's four basic types — bool, int, float and string: what values each can hold, how to write them, how big a number can get, why 0.1 + 0.2 isn't exactly 0.3, and why money is safest stored in pence.

14–16 min

The Problem in Real Life

Olivia adds up the week: a 10p booking fee and a 20p card fee per online booking, so 30p each. Mia writes a check to be sure the code agrees: if ($bookingFee + $cardFee == 0.3). It says no.

She prints the sum: 0.3. She prints the check again: false. Then Marcus shows her one more line: var_dump(0.1 + 0.2);. PHP answers float(0.30000000000000004).

"Computers store decimal numbers in a way that's almost always nearly right," says Marcus. "For most things, nearly is fine. For money, it isn't. Let's look at the four basic types properly."

M

0.1 plus 0.2 isn't 0.3? How can a computer get basic maths wrong?

Mia

Numbers on Paper vs. Numbers in the Computer

Pennies that don't add up

Decimal numbers like 0.1 can't be stored exactly, so tiny errors appear.

Two kinds of numbers

int and float look alike, but they behave differently — 7 / 2 gives a float, 6 / 2 an int.

Text that looks like a number

"25" is a string, not a number. And a string's length counts bytes, not letters.

Scalar Types: bool, int, float and string

Scalar types: a scalar value is a single, simple value — one number, one piece of text, one yes/no. PHP has four scalar types, and they're the building blocks of everything else:

  • bool: true or false.
  • int: a whole number, like 25, 0 or -3.
  • float: a number with a decimal part, like 12.5.
  • string: text, like "Tom's Barbershop" — or "25", which looks like a number but is text.

bool: a bool (short for boolean) has only two possible values: true and false. You can write them in any case, but lower case is the convention. You rarely type them yourself — most bools come from questions your code asks, like 5 > 3 ("is 5 greater than 3?"), which gives true. Bools are what if decides with (Act 06). Name bool variables like a yes/no question: $isOpen, $hasPaid.

bools usually come from questions
<?php
$isOpen = 5 > 3; // "is 5 greater than 3?"
var_dump($isOpen); // bool(true)
$hasPaid = false;
var_dump($hasPaid); // bool(false)

int: an int (integer) is a whole number, positive or negative, with no decimal point. On a normal 64-bit server it can go from about minus 9.2 quintillion to plus 9.2 quintillion — PHP_INT_MAX is 9223372036854775807. Ints can be written in several ways, shown below: the normal way (25), with underscores to make long numbers readable (1_000_000 — the underscores are ignored), and in hexadecimal (0x1A), octal (0o17) and binary (0b101) — other number systems you'll mostly meet in low-level code. If a calculation goes past PHP_INT_MAX, PHP doesn't crash: it quietly switches the result to a float.

Ways to write an int
<?php
var_dump(25); // int(25)
var_dump(-3); // int(-3)
var_dump(1_000_000); // int(1000000): underscores just help you read
var_dump(0x1A); // int(26): hexadecimal
var_dump(0o17); // int(15): octal
var_dump(0b101); // int(5): binary

float: a float (floating-point number) is a number that can have a decimal part: 12.5, 0.1, -3.75. You can also write very big or very small floats in scientific notation: 1.5e3 means 1.5 × 1000, so 1500.0. Floats appear in calculations too: / (divide) gives a float when the result isn't whole — 7 / 2 is 3.5 — and an int when it is: 6 / 2 is 3. To divide and keep only the whole part, use intdiv(7, 2), which gives 3.

Floats, and what / gives
<?php
var_dump(12.50); // float(12.5)
var_dump(1.5e3); // float(1500)
var_dump(7 / 2); // float(3.5): not a whole result
var_dump(6 / 2); // int(3): a whole result
var_dump(intdiv(7, 2)); // int(3): only the whole part

Why 0.1 + 0.2 isn't 0.3: computers store floats in binary (only 0s and 1s). Just as 1/3 can't be written exactly in decimal (0.3333…), many decimal numbers — including 0.1 and 0.2 — can't be stored exactly in binary. The computer stores the closest value it can, and tiny errors add up: 0.1 + 0.2 is really 0.30000000000000004. That's not a PHP bug; every language does the same. Two rules follow, shown below: never compare floats with ==, and never store money as a float. Keep money in the smallest unit as an int — pence (or cents): £12.50 becomes 1250. Ints are always exact. You only divide by 100 when you show the price.

The 0.1 + 0.2 problem
<?php
var_dump(0.1 + 0.2); // float(0.30000000000000004)
var_dump(0.1 + 0.2 == 0.3); // bool(false)
Money in pence: always exact
<?php
$bookingFeePence = 10;
$cardFeePence = 20;
$totalPence = $bookingFeePence + $cardFeePence;
var_dump($totalPence === 30); // bool(true): ints are exact
$beardTrimPence = 1250; // £12.50
echo "Beard trim: £", number_format($beardTrimPence / 100, 2), "\n"; // Beard trim: £12.50

=== means "exactly equal, same type too" (Act 05). number_format() shows a number with 2 decimal places — only for display.

string: a string is a sequence of characters — text. You write it in single or double quotes (Act 02, chapter 5). Digits in quotes are a string, not a number: "25" is text that happens to contain the characters 2 and 5. One detail that surprises people: PHP measures strings in bytes, not letters. Plain English letters take 1 byte each, but letters like ë, é or £ take 2 or more. So strlen("Tom") is 3, but strlen("Zoë") is 4. Act 07 shows how to count letters properly and covers strings in depth.

Strings count bytes, not letters
<?php
var_dump("25"); // string(2) "25": text, not a number
var_dump(strlen("Tom")); // int(3)
var_dump(strlen("Zoë")); // int(4): ë takes 2 bytes

Seeing the type: var_dump() (Act 02, chapter 7) always shows the type before the value: bool(true), int(25), float(12.5), string(2) "25". Whenever you're unsure what type a value has, dump it. The table below sums up the four scalar types.

Table — The four scalar types
TypeHoldsExamplesvar_dump shows
booltrue or falsetrue, false, 5 > 3bool(true)
intWhole numbers25, -3, 1_000_000, 0x1Aint(25)
floatNumbers with a decimal part12.5, 0.1, 1.5e3, 7 / 2float(12.5)
stringText (bytes)"Tom", 'Zoë', "25"string(3) "Tom"

Key Takeaway

PHP has four scalar types: bool (true/false), int (whole numbers, up to PHP_INT_MAX), float (decimal numbers, stored approximately) and string (text, measured in bytes). / gives a float unless the result is whole. Floats can't store most decimals exactly, so never compare them with == and never keep money in them — use whole pence as ints.

Why This Matters

Choosing the right type is the first decision for every value Mia stores. Prices in pence as ints, yes/no answers as bools, names as strings — get that right, and a whole family of bugs (lost pennies, wrong comparisons, odd rounding) never happens.

Every payment system BookBay will ever talk to works in the smallest currency unit for exactly this reason. Knowing why will make those systems (and the database chapters in Act 18) feel natural.

Mia switches all prices to pence, and the fees add up exactly. Then she dumps a whole booking from the partner site, and the output contains things that aren't bool, int, float or string at all: a list in square brackets, something called object(stdClass), and a strange resource(2) of type (stream). That's the next chapter.