Priority of Mathematical Operations in PHP
Mathematical operations in PHP have the same
priority as in regular mathematics. That is,
multiplication and division are performed first,
and only then addition and subtraction. In the following
example, 2
will first be multiplied by 2
and then 3
will be added to the result:
<?php
$a = 2 * 2 + 3;
echo $a; // outputs 7 (result of 4 + 3)
?>
Without running the code, determine what will be displayed on the screen:
<?php
$a = 5 + 5 * 3;
echo $a;
?>
Without running the code, determine what will be displayed on the screen:
<?php
$a = 5 + 5 * 3 + 3;
echo $a;
?>
Without running the code, determine what will be displayed on the screen:
<?php
$a = 8 / 2 + 2;
echo $a;
?>
Without running the code, determine what will be displayed on the screen:
<?php
$a = 8 + 2 / 2;
echo $a;
?>