PHP Operator Precedence & Associativity Table
Notes:
PHP Precedence & Associativity of PHP Operators
Precedence:
- determines while evaluating any expression; which operator should be evaluated first before the other? I.e. which operator needs to be given higher precedence than the other?
Associativity:
- determines if same operator appears one or more times in an expression, in which direction the specific operator need to be evaluated
$a = 3 * 5 + 4 / 2 + 3 + 20 -10
Note: Operators with equal precedence and non-associative should not be used Nextto each other. Ex: 1 < 2 > 3
Example code:
$a = 2 + 2 + 2;
echo $a,"<br/>"; // 6
$b = 2 + 3 * 5;
echo $b,"<br/>"; // 17
$c = 3 * 5 + 4 / 2 + 3; // 20
echo $c,"<br/>";
$d = 3 * (5 +4 ) / 2 +3; // 16.5
echo $d,"<br/>";
$e = 3 * 5 + 4 / 2 + 3 + 20 -10; // 30
echo $e,"<br/>";
$f = "Score: " . 2 + 2; // 2
echo $f,"<br/>";
$g = "Score: " . (2 + 2); // Score: 4
echo $g,"<br/>";
$h = 2 + 2 . "Score: "; // 4Score:
echo $h,"<br/>";
Interview Questions:
1. PHP stands for ______________
a. Hypertext Preprocessor
b. Preprocessor Hypertext
c. Personal Home Processor
d. Personal Hypertext processor
Ans: a