Numbers

A number is written as it reads: 42, 3.5, -7. Whole numbers stay exact however large they grow, so 2 ** 100 gives every digit, and numbers with a decimal point work to about 15 significant digits.

Number methods give a new number and never change a variable, so keep the result: $total = $total->round(digits: 2).

A minus written straight onto a number is part of it, so -5->abs() is 5. With a variable, the minus applies to the whole chain: -$change->abs() is minus the size of the change, and (-$change)->abs() brackets it first.

To check that a number is inside a range, chain comparisons as you would in maths. Both ends count with <=:

$age = 18
echo 13 <= $age <= 19   // true because 18 is a teenage age
echo 0 <= $age <= 17    // false because 18 is past 17

Round: round · floor · ceil · to_int

Compare and limit: min · max · clamp · abs · is_even · is_odd

Bits: bits

Count: increment · decrement · times

Turn into something else: format · to_date · to_number

->abs()

->abs() gives the number without its minus sign: how far it is from zero. It suits a difference where you care how big it is but not which way it went:

$change = 4 - 9
echo $change          // -5
echo $change->abs()   // 5 because the sign is dropped
echo -5->abs()        // 5 because -5 is one number, and its size is 5

->bits()

->bits(and|or|xor|shift: number) or ->bits(invert) works on the binary digits of a whole number, one step per call. It suits flags packed into one number, where each bit means one thing:

$read = 4
$write = 2
$permissions = 6                              // read and write, because 4 + 2 is 6
echo $permissions->bits(and: $read) == $read // true because 6 has the 4 bit set
echo $permissions->bits(or: 1)               // 7 because or: turns the 1 bit on
echo $permissions->bits(xor: $write)         // 4 because xor: flips the 2 bit off
echo $permissions->bits(shift: 1)            // 12 because every bit moves one place left
echo $permissions->bits(shift: -1)           // 3 because a negative shift moves right
echo $permissions->bits(invert)              // -7 because every bit flips

->ceil()

->ceil() rounds up to the next whole number, or leaves a whole number as it is. It suits counting what you need to hold everything:

$people = 23
echo ($people / 4)->ceil()   // 6 because 23 / 4 is 5.75, and 5 tables of 4 would leave 3 people standing
echo 6->ceil()               // 6 because it is already whole
echo -2.5->ceil()          // -2 because up means towards the larger number

->clamp()

->clamp(min, max) keeps the number inside min..max. Below min it gives min, above max it gives max, and in between it gives the number:

echo 150->clamp(0, 100)   // 100 because 150 is above the top
echo -20->clamp(0, 100) // 0 because -20 is below the bottom
echo 42->clamp(0, 100)    // 42 because it is already in range

->decrement()

->decrement() is the number minus 1. ->increment() adds 1:

$lives = 3
$lives = $lives->decrement()
echo $lives   // 2

->floor()

->floor() rounds down to the whole number below, or leaves a whole number as it is. It suits counting how many whole things fit:

$budget = 20
echo ($budget / 3)->floor()   // 6 because 20 / 3 is 6.67, and only 6 whole tickets at £3 fit in £20
echo 3.99->floor()            // 3 because the decimals are always dropped downwards
echo -2.5->floor()          // -3 because down means towards the smaller number

->format()

->format(pattern?) turns the number into text for people to read. With no pattern it gives a whole number with commas between thousands. A pattern picks another layout: N2 keeps two decimal places, P0 shows a share as a percentage, and 0.00 or 000 pad with zeros:

echo 1234567->format()           // '1,234,567' because the commas split it into thousands
echo 1234.567->format('N2')      // '1,234.57' because N2 keeps two decimal places, rounded
echo 0.075->format('P1')         // '7.5 %' because P shows a share of 1 as a percentage
echo 7->format('000')            // '007' because each 0 is a digit that must be shown

->increment()

->increment() is the number plus 1. ->decrement() takes 1 away:

$visits = 41
$visits = $visits->increment()
echo $visits   // 42

->is_even()

->is_even() is true for a whole number that 2 divides exactly. A number with decimals is neither even nor odd:

echo 10->is_even()        // true
echo 7->is_even()         // false because 7 leaves 1 over when shared into twos
echo -4->is_even()      // true because negative numbers can be even too
echo 2.5->is_even()       // false because it isn't a whole number

->is_odd()

->is_odd() is true for a whole number that leaves 1 over when divided by 2. It suits striping every other row:

$rows = ['tea', 'milk', 'juice']
echo $rows->map(fn(drink, index) => $index->is_odd() ? 'grey' : 'white')   // ['white', 'grey', 'white'] because rows 0 and 2 are even
echo 2.5->is_odd()   // false because it isn't a whole number

->max()

->max(other) gives the larger of the number and other. It suits setting a floor: the answer is never below other:

$balance = -12
echo $balance->max(0)   // 0 because 0 is larger than -12, so the shown balance never goes below 0
echo 30->max(0)         // 30

->min()

->min(other) gives the smaller of the number and other. It suits setting a ceiling: the answer is never above other:

$wanted = 12
$in_stock = 10
echo $wanted->min($in_stock)   // 10 because you can't sell more than the 10 there are

->round()

->round(digits?) rounds to the nearest whole number, or to digits decimal places. A number exactly halfway rounds away from zero, as it would on paper:

echo 3.14159->round(digits: 2)   // 3.14 because the next digit, 1, rounds down
echo 2.5->round()                // 3 because halfway rounds away from zero
echo -2.5->round()             // -3 because halfway rounds away from zero on the negative side too
echo 9.996->round(digits: 2)     // 10 because 9.996 is nearer 10.00 than 9.99

->times()

->times(fn(i)) calls the function once for each whole number from 0 up to one below the number, and gives back a list of what it returned. It suits building a list of a fixed length:

echo 3->times(fn(i) => $i * $i)          // [0, 1, 4] because i is 0, then 1, then 2
echo 4->times(fn(i) => "Seat { $i + 1 }")->last() // 'Seat 4' because i stops at 3
echo 0->times(fn(i) => $i)               // [] because there is nothing to count

->to_date()

->to_date() reads the number as a Unix timestamp, the number of seconds since the start of 1 January 1970 in UTC, which is how many systems and APIs send dates. $date->timestamp() turns a date back into one. Only whole numbers can become dates this way; to move a date along, add a duration such as 3d instead:

$sent = 1795996800->to_date()
echo $sent->format('d MMMM yyyy')   // '30 November 2026'
echo $sent->timestamp()             // 1795996800 because ->timestamp() reverses ->to_date()

->to_int()

->to_int() drops everything after the decimal point, leaving the whole number part. Unlike ->floor(), it goes towards zero for a negative number:

echo 9.99->to_int()      // 9 because the .99 is dropped, not rounded
echo -3.7->to_int()    // -3 because the decimals are dropped, which moves towards zero
echo -3.7->floor()     // -4 because ->floor() always goes down

->to_number()

->to_number(default?) gives the number itself. It is there so that a value that might be a number or text can call ->to_number() either way. On text it reads the number written there:

echo 42->to_number()                  // 42
echo '12.5'->to_number() + 1          // 13.5 because the text is read as a number first
echo 'none'->to_number(0)             // 0 because 'none' isn't a number, so the default is used

Edit this page on GitHub