PHP/Operator/Operator Precedence
Содержание
Operator Precedence
<source lang="html4strict">
Associativity Operators left , left or left xor left and right print left = += -= *= /= .=%=&= |= ^= ~= «= »= left ? : left || left && left | left ^ left & non-associative == != === non-associative < <= > >= left << >> left + - . left * /% right ! ~ ++ � (int) (double) (string) (array) (object) @ right [ non-associative new
</source>
Operator Precedence summary table
<source lang="html4strict">
Operator Class Associativity new Unary n/a [ Unary Right ! Unary ! and ~ are right associative; ~ ++ and-- operators are right or left associative ++ -- (int) (double) (string) (array) (object)
@
- / % Binary Left
+ - . Binary Left << >> Binary Left < <= > >= Binary n/a == != === !== Binary n/a & Binary Left ^ Binary Left | Binary Left && Binary Left || Binary Left ?: Ternary Left = Binary Left += -=
- =
/= .= %= &= |= ^= ~= <<= >>=
print Unary Right and Binary Left xor Binary Left or Binary Left , n/a Left
</source>
Operators, precedence, and their associativity
<source lang="html4strict">
Operators Associativity
, Left "$x, $y, $z" is "($x, $y), $z"
or Left "$x OR $y OR $z" is "($x OR $y) OR $z"
xor left "x XOR y XOR z" is "($x XOR $y) XOR $z"
and Left "x AND y AND z" is "(x AND y) AND z"
= += -= * = /= .= %= &= |= ^= <<= gt;>= Right "$x /= $y /= $z" is "$x /= ($y /= $z)"
? : Left || Left "$x || $y || $z" is "($x || $y) || $z"
&& Left "$x && $y && $z" is "($x && $y) && $z"
| Left "$x | $y | $z" is "($x | $y) | $z"
^ Left "$x ^ $y ^ $z" is "($x ^ $y) ^ $z"
& Left "$x & $y & $z" is "($x & $y) & $z"
== != === !== Non-associative
< < = > >= Non-associative
<< >> Left "$x >> $y >> $z" is "($x >> $y) >> $z"
+ - . Left "$x - $y - $z" is "($x - $y) - $z"
- / % Left "$x / $y / $z" is "($x / $y) / $z"
! ~ ++ -- (int) (float) (string)(array) (object) @ Right
[ Right new Non-associative
</source>
Using the ? Operator
<source lang="html4strict">
The ?, or ternary, operator returns a value derived from one of two expressions separated by a colon. (expression) ?returned_if_expression_is_true:returned_if_expression_is_false; If the test expression evaluates to true, the result of the second expression is returned; otherwise, the value of the third expression is returned.
<html> <body>
<?php
$satisfied = "no"; $pleased = "very"; $sorry = "sorry"; $text = ( $satisfied=="very" )?$pleased:$sorry; print "$text";
?>
</body> </html>
</source>
Using the ? operator to create a message
<source lang="html4strict">
<?php $logged_in = TRUE; $user = "Admin"; $banner = ($logged_in==TRUE)?"Welcome back, $user!":"Please login."; echo "$banner"; ?>
</source>