Values and Types
Numbers
PHP numbers handle arithmetic for counts, totals, and measurements.
arithmetic
Number expressions combine values with operators such as `+`, `-`, `*`, and `/`.
Numbers
numbers.php
Replay: real traced execution (multi-file project)
<?php
$apples = 7;
$oranges = 5;
$total = $apples + $oranges;
$average = $total / 2;
echo "apples=" . $apples . "\n";
echo "total=" . $total . "\n";
echo "average=" . $average . "\n";
<?php
$apples = 3;
$oranges = 5;
$total = $apples + $oranges;
$average = $total / 2;
echo "apples=" . $apples . "\n";
echo "total=" . $total . "\n";
echo "average=" . $average . "\n";
<?php
$apples = 10;
$oranges = 5;
$total = $apples + $oranges;
$average = $total / 2;
echo "apples=" . $apples . "\n";
echo "total=" . $total . "\n";
echo "average=" . $average . "\n";
$apples ← 7, $oranges ← 5, $total ← 12, $average ← 6
1<?php2$apples→ 7 = 7; //@apples=3, 103$oranges→ 5 = 5;4$total→ 12 = $apples7 + $oranges5;5$average→ 6 = $total12 / 2;67echo "apples=" . $apples7 . "\n";8echo "total=" . $total12 . "\n";9echo "average=" . $average6 . "\n";outputapples=7 total=12 average=6
$apples ← 3, $oranges ← 5, $total ← 8, $average ← 4
1<?php2$apples→ 3 = 3;3$oranges→ 5 = 5;4$total→ 8 = $apples3 + $oranges5;5$average→ 4 = $total8 / 2;67echo "apples=" . $apples3 . "\n";8echo "total=" . $total8 . "\n";9echo "average=" . $average4 . "\n";outputapples=3 total=8 average=4
$apples ← 10, $oranges ← 5, $total ← 15, $average ← 7.5
1<?php2$apples→ 10 = 10;3$oranges→ 5 = 5;4$total→ 15 = $apples10 + $oranges5;5$average→ 7.5 = $total15 / 2;67echo "apples=" . $apples10 . "\n";8echo "total=" . $total15 . "\n";9echo "average=" . $average7.5 . "\n";outputapples=10 total=15 average=7.5
Follow the Numbers
$applesstarts at7.$orangesis5.$total = $apples + $orangesbecomes12.$average = $total / 2becomes6.- The program prints
apples=7,total=12, andaverage=6. | apples | oranges | total | average | | --- | --- | --- | --- | | 3 | 5 | 8 | 4 | | 7 | 5 | 12 | 6 | | 10 | 5 | 15 | 7.5 |
Exercise: numbers.php
Reproduce total=12 and average=6, then use the pinned apples values 3 and 10 to predict each total and average.