PHP/Operator/Operator Precedence
Содержание
Operator Precedence
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
Operator Precedence summary table
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
Operators, precedence, and their associativity
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
Using the ? Operator
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>
<div>
<?php
$satisfied = "no";
$pleased = "very";
$sorry = "sorry";
$text = ( $satisfied=="very" )?$pleased:$sorry;
print "$text";
?>
</div>
</body>
</html>
Using the ? operator to create a message
<?php
$logged_in = TRUE;
$user = "Admin";
$banner = ($logged_in==TRUE)?"Welcome back, $user!":"Please login.";
echo "$banner";
?>