Integers and floats
- int: whole numbers like
42 - float: decimals like
3.14 - Division can return a float:
7 / 2→3.5 - Whole-number division:
intdiv(7, 2)→3 - Remainder:
7 % 2→1
Math functions
| Function | Result |
|---|---|
round(4.567, 2) |
4.57 |
floor(4.9) |
4 |
ceil(4.1) |
5 |
abs(-8) |
8 |
max(3, 9, 5) / min(...) |
9 / 3 |
sqrt(81) |
9 |
pow(2, 10) or 2 ** 10 |
1024 |
random_int(1, 6) |
random 1–6 |
Formatting
number_format(150000) → 150,000. For Indian style (1,50,000) use NumberFormatter with en_IN or a small helper.
Money tip
Floats can be slightly imprecise (0.1 + 0.2). Store money in paise as integers, or round before showing.
Example
Example — Bill calculator with GST
PHP
<?php
$items = ['Notebook' => 60, 'Pen' => 15, 'Bag' => 899];
$subtotal = array_sum($items);
$gst = round($subtotal * 0.18, 2);
$total = $subtotal + $gst;
echo 'Subtotal: ₹' . number_format($subtotal, 2) . '<br>';
echo 'GST 18%: ₹' . number_format($gst, 2) . '<br>';
echo 'Total: ₹' . number_format($total, 2) . '<br>';
echo 'Rounded up: ₹' . ceil($total) . '<br>';
echo 'Lucky draw number: ' . random_int(1, 100) . '<br>';
echo 'EMI for 3 months: ₹' . round($total / 3, 2);
Output: Subtotal: ₹974.00 / GST 18%: ₹175.32 / Total: ₹1,149.32 / Rounded up: ₹1150 / Lucky draw number: (random) / EMI for 3 months: ₹383.11