Numbers & Math

From a shopping cart total to a dice roll, numbers are everywhere. This lesson covers arithmetic and modulo, rounding, money-friendly formatting, the built-in math functions, secure randomness, and the one floating-point trap you absolutely must know.

Learn Numbers & Math in our free PHP course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick recall.

Part of the free Php course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

What You'll Learn in This Lesson

1️⃣ Arithmetic & Modulo

PHP has the usual operators plus two you'll lean on: modulo ( % ) gives the remainder of a division — perfect for "is this even?" or wrapping around a cycle — and the exponent operator ( ** ). Remember the rule from Data Types: / always returns a float, so use intdiv() for whole-number division.

2️⃣ Rounding

Three functions cover almost every need. round() goes to the nearest value (with optional decimal precision), ceil() always rounds up, and floor() always rounds down. abs() removes the sign.

3️⃣ Formatting Numbers

Raw numbers look unfriendly to humans. number_format() adds thousands separators and a fixed number of decimals — the go-to for prices, statistics, and dashboards. You can even swap the separators for international formats.

4️⃣ The Math Library

PHP bundles a complete set of math helpers: max / min , sqrt , pow , constants like M_PI and PHP_INT_MAX , and — crucially for anything security-related — random_int() , which is cryptographically secure.

5️⃣ The Floating-Point Trap ⚠️

This is the single most important thing in the lesson. Because floats are stored in binary, simple decimals like 0.1 can't be represented exactly, so 0.1 + 0.2 is not exactly 0.3 . Never compare floats with === . Compare within a tiny tolerance, and for money work in integer cents or use bcmath .

Now you try. Fill the ___ blanks to total a small cart and format the result as money.

These scrambled lines should compute an average and print it rounded to one decimal. Put them in the order that produces Average: 7.7 .

First create the array, then sum it, then divide the sum by the count to get the average, and only then round and print. Each line depends on the variable produced by the line before it. The exact average is 7.666…, which round($avg, 1) turns into 7.7 .

Predict the output before revealing each answer.

Prints 2 . Modulo returns the remainder: 17 divided by 5 is 3 with 2 left over.

Prints bool(false) . Binary floating point can't represent 0.1 or 0.2 exactly, so the sum is 0.30000000000000004. Compare floats with an epsilon, never with === .

Prints 4 3 . ceil always rounds up to 4; floor always rounds down to 3.

📋 Quick Reference — Numbers & Math

Write it yourself, run it on onecompiler.com/php or your own machine, then check against the expected output in the comments.

Practice quiz

What does echo 17 % 5; print?

  • 3
  • 3.4
  • 2
  • 12

Answer: 2. Modulo returns the remainder: 17 divided by 5 is 3 with 2 left over.

What does echo 17 / 5; print?

  • 3.4
  • 3
  • 2
  • 4

Answer: 3.4. The / operator always returns a float, so 17 / 5 is 3.4. Use intdiv() for whole-number division.

What does var_dump(0.1 + 0.2 === 0.3); output?

  • bool(true)
  • float(0.3)
  • An error
  • bool(false)

Answer: bool(false). Binary floats can't represent 0.1 or 0.2 exactly, so the sum is 0.30000000000000004. Never compare floats with ===.

What does echo round(2.5); print?

  • 2
  • 3
  • 2.5
  • 4

Answer: 3. round() defaults to 'round half away from zero', so 2.5 becomes 3.

What does echo ceil(3.1) . ' ' . floor(3.9); print?

  • 4 3
  • 3 4
  • 4 4
  • 3 3

Answer: 4 3. ceil always rounds up (3.1 -> 4); floor always rounds down (3.9 -> 3).

What does echo number_format(1234567.891, 2); print?

  • 1234567.89
  • 1.234.567,89
  • 1,234,567.89
  • 1,234,568

Answer: 1,234,567.89. number_format with 2 decimals adds comma thousands separators: 1,234,567.89.

What does echo intdiv(17, 5); print?

  • 3.4
  • 3
  • 2
  • 4

Answer: 3. intdiv does true integer division — no remainder, no float — so 17 intdiv 5 is 3.

Which function should you use for cryptographically secure random numbers?

  • rand()
  • mt_rand()
  • round()
  • random_int()

Answer: random_int(). random_int() is cryptographically secure and uniformly distributed; rand() and mt_rand() are predictable.

What does echo 2 ** 10; print?

  • 20
  • 1024
  • 100
  • 1000

Answer: 1024. ** is the exponent operator: 2 to the 10th power is 1024.

What does the result of number_format() return that affects further math?

  • A float you can keep calculating with
  • An integer
  • A string with separators — format only at display time
  • An array of digits

Answer: A string with separators — format only at display time. number_format returns a string with commas, so use it only at display time; calculate with the raw number.