Beginner Fundamentals

PHP Numbers

PHP works with two main numeric types: integers for whole numbers and floats for decimals. You can perform calculations and use built-in math functions.

Integers and floats

<?php
$whole = 25;     // integer
$decimal = 4.5;  // float

var_dump($whole);   // int(25)
var_dump($decimal); // float(4.5)

Math operations

Use the standard arithmetic operators.

<?php
echo 10 + 3;  // 13
echo 10 - 3;  // 7
echo 10 * 3;  // 30
echo 10 / 3;  // 3.3333...
echo 10 % 3;  // 1 (remainder)

Useful number functions

<?php
echo abs(-7);        // 7 (absolute value)
echo round(3.14159, 2); // 3.14 (round to 2 decimals)
echo intdiv(10, 3);  // 3 (integer division)
echo max(4, 9, 2);   // 9
echo min(4, 9, 2);   // 2

Quick reference

  • abs() returns the absolute value
  • round() rounds to a given precision
  • intdiv() divides and returns a whole number
  • max() / min() find the largest or smallest value