# php7运算符

• 运算符优先级
• 运算符优先级指定了两个表达式绑定得有多“紧密”。例如，表达式1 + 5 * 3的结果是16而不是18是因为乘号（“*”）的优先级比加号（“+”）高。必要时可以用括号来强制改变优先级。例如：(1 + 5)* 3的值为18。如果运算符优先级相同，那运算符的结合方向决定了该如何运算。例如，"-&
• 算术运算符
• 还记得学校里学到的基本数学知识吗？就和它们一样。算术运算符例子名称结果-\$a取反\$a的负值。\$a +\$b加法\$a和\$b的和。\$a -\$b减法\$a和\$b的差。\$a *\$b乘法\$a和\$b的积。\$a /\$b除法\$a除以\$b的商。
• 赋值运算符
• 基本的赋值运算符是“=”。一开始可能会以为它是“等于”，其实不是的。它实际上意味着把右边表达式的值赋给左边的运算数。赋值运算表达式的值也就是所赋的值。也就是说，“\$a = 3”的值是 3。这样就可以做一些小技巧：<?php\$a = (\$b = 4) + 5; // \$a
• 位运算符
• 位运算符允许对整型数中指定的位进行求值和操作。位运算符例子名称结果\$a &\$bAnd（按位与）将把\$a和\$b中都为 1 的位设为 1。\$a |\$bOr（按位或）将把\$a和\$b中任何一个为 1 的位设为 1。\$a ^\$bXor（按位异或）将把\$a和\$b中一个为 1 另一个为 0 的位设为 1。
• 比较运算符
• 比较运算符，如同它们名称所暗示的，允许对两个值进行比较。还可以参考PHP 类型比较表看不同类型相互比较的例子。比较运算符例子名称结果\$a ==\$b等于TRUE，如果类型转换后\$a等于\$b。\$a ===\$b全等TRUE，如果\$a等于\$b，并且它们的类型也相同。\$a !=\$b不等TRUE，如果类型转换后
• 错误控制运算符
• PHP 支持一个错误控制运算符：@。当将其放置在一个 PHP 表达式之前，该表达式可能产生的任何错误信息都被忽略掉。如果用set_error_handler()设定了自定义的错误处理函数，仍然会被调用，但是此错误处理函数可以（并且也应该）调用error_reporting()，而该函数在出错语句前
• 执行运算符
• PHP 支持一个执行运算符：反引号（``）。注意这不是单引号！PHP 将尝试将反引号中的内容作为 shell 命令来执行，并将其输出信息返回（即，可以赋给一个变量而不是简单地丢弃到标准输出）。使用反引号运算符“`”的效果与函数shell_exec()相同。<?php\$output =
• 递增／递减运算符
• PHP 支持 C 风格的前／后递增与递减运算符。Note:递增／递减运算符不影响布尔值。递减NULL值也没有效果，但是递增NULL的结果是1。递增／递减运算符例子名称效果++\$a前加\$a的值加一，然后返回\$a。\$a++后加返回\$a，然后将\$a的值加一。--\$a前减\$a的值减一，然后返回\$a
• 逻辑运算符
• 逻辑运算符例子名称结果\$a and \$bAnd（逻辑与）TRUE，如果\$a和\$b都为TRUE。\$a or \$bOr（逻辑或）TRUE，如果\$a或\$b任一为TRUE。\$a xor \$bXor（逻辑异或）TRUE，如果\$a或\$b任一为
• 字符串运算符
• 有两个字符串（string）运算符。第一个是连接运算符（“.”），它返回其左右参数连接后的字符串。第二个是连接赋值运算符（“.=”），它将右边参数附加到左边的参数之后。更多信息见赋值运算符。<?php\$a = "Hello ";\$b = \$a . "World!"; /
• 数组运算符
• 数组运算符例子名称结果\$a +\$b联合\$a和\$b的联合。\$a ==\$b相等如果\$a和\$b具有相同的键／值对则为TRUE。\$a ===\$b全等如果\$a和\$b具有相同的键／值对并且顺序和类型都相同则为TRUE。\$a !=\$b不等如果\$a不等于\$b则为
• 类型运算符
• instanceof用于确定一个 PHP 变量是否属于某一类class的实例：Example #1 对类使用instanceof<?phpclass MyClass{}class NotMyClass{}\$a = new MyClass;var_

PHP 的运算符完整列表见下节运算符优先级。该节也解释了运算符优先级和结合方向，这控制着在表达式包含有若干个不同运算符时究竟怎样对其求值。

```of course this should be clear, but i think it has to be mentioned espacially:
AND is not the same like &&
for example:
<?php \$a && \$b || \$c; ?>
is not the same like
<?php \$a AND \$b || \$c; ?>
the first thing is
(a and b) or c
the second
a and (b or c)
'cause || has got a higher priority than and, but less than &&
of course, using always [ && and || ] or [ AND and OR ] would be okay, but than you should at least respect the following:
<?php \$a = \$b && \$c; ?>
<?php \$a = \$b AND \$c; ?>
the first code will set \$a to the result of the comparison \$b with \$c, both have to be true, while the second code line will set \$a like \$b and THAN - after that - compare the success of this with the value of \$c
maybe usefull for some tricky coding and helpfull to prevent bugs :D
greetz, Warhog```
```If you use "AND" and "OR", you'll eventually get tripped up by something like this:
<?php
\$this_one = true;
\$that = false;
\$truthiness = \$this_one and \$that;
?>
Want to guess what \$truthiness equals?
If you said "false" ...it's wrong!
"\$truthiness" above has the value "true". Why? "=" has a higher precedence than "and". The addition of parentheses to show the implicit order makes this clearer:
<?php
(\$truthiness = \$this_one) and \$that;
?>
If you used "&&" instead of and in the first code example, it would work as expected and be "false".
This also works to get the correct value, as parentheses have higher precedence than "=":
<?php
\$truthiness = (\$this_one and \$that);
?>
```
```Other Language books' operator precedence section usually include "(" and ")" - with exception of a Perl book that I have. (In PHP "{" and "}" should also be considered also). However, PHP Manual is not listed "(" and ")" in precedence list. It looks like "(" and ")" has higher precedence as it should be.
Note: If you write following code, you would need "()" to get expected value.
<?php
\$bar = true;
\$str = "TEST". (\$bar ? 'true' : 'false') ."TEST";
?>
Without "(" and ")" you will get only "true" in \$str.
(PHP4.0.4pl1/Apache DSO/Linux, PHP4.0.5RC1/Apache DSO/W2K Server)
It's due to precedence, probably.```
`The variable symbol '\$' should be considered as the highest-precedence operator, so that the variable variables such as \$\$a won't confuse the parser. [http://www.php.net/manual/en/language.variables.variable.php]`
```Note that in php the ternary operator ?: has a left associativity unlike in C and C++ where it has right associativity.
You cannot write code like this (as you may have accustomed to in C/C++):
<?php
\$a = 2;
echo (
\$a == 1 ? 'one' :
\$a == 2 ? 'two' :
\$a == 3 ? 'three' :
\$a == 4 ? 'four' : 'other');
echo "\n";
// prints 'four'
?>
You need to add brackets to get the results you want:
<?php
\$a = 2;
echo (\$a == 1 ? 'one' :
(\$a == 2 ? 'two' :
(\$a == 3 ? 'three' :
(\$a == 4 ? 'four' : 'other') ) ) );
echo "\n";
//prints 'two'
?>
```
`The scope resolution operator ::, which is missing from the list above, has higher precedence than [], and lower precedence than 'new'. This means that self::\$array[\$var] works as expected.`
```A quick note to any C developers out there, assignment expressions are not interpreted as you may expect - take the following code ;-
<?php
\$a=array(1,2,3);
\$b=array(4,5,6);
\$c=1;
\$a[\$c++]=\$b[\$c++];
print_r( \$a ) ;
?>
This will output;-
Array (  => 1  => 6  => 3 )
as if the code said;-
\$a=\$b;
Under a C compiler the result is;-
Array (  => 1  => 5  => 3 )
as if the code said;-
\$a=\$b;
It would appear that in php the increment in the left side of the assignment is processed prior to processing the right side of the assignment, whereas in C, neither increment occurs until after the assignment.```
```Simple POST and PRE incremnt sample:
<?php
\$b = 5;
\$a = ( ( ++\$b ) > 5 ); // Pre-increment test
echo (int)\$a;
\$b = 5;
\$a = ( ( \$b++ ) > 5 ); // Post-increment test
echo (int)\$a;
?>
This will output 10, because of the difference in post- and pre-increment operations```