# 运算符与错误控制符@

## 错误控制符@

php支持一个错误运算符：@。当将其放置在一个PHP表达式之前，该表达式可能产生任何错误信息都被忽略掉。不建议使用。

## 运算符

[官方文档](https://secure.php.net/manual/zh/language.operators.precedence.php)

**运算符优先级：**  <br>

**递增/递减>!>算数运算符>大小比较>(不)相等比较**>引用>位运算符(^)>**逻辑与>逻辑或>三目运算>赋值**>and>xor>or

> 括号的使用可以增加代码可读性

* 递增/递减运算符不影响布尔值

  (也就是递增递减 true  和false)
* 递减NULL 没有效果
* 递增NULL值为1

递增和递减在前就先运算符后返回，反之就先返回，后运算。

```php
$a = true;
if($a++) {
    echo "true1";  // 执行
}

if($a--) {
    echo "true2"; // 执行
}
$b = false;
if($b++){
    echo "false1"; // 不执行
}
var_dump($b);   // false
if($b--){
    echo "false1"; // 不执行
}
var_dump($b);   // false

 $c = null;
echo --$c;
var_dump($c); // null
echo ++$c; // 输出 1
```

**特别注意短路作用：**  <br>

当逻辑与时，如果前面为false，则后面不做执行

当逻辑或，如果前面为true，则后面不在做执行

```php
//相等比较 > 逻辑与 && > 逻辑或 || > 赋值
echo $a = true || $b == 3  // true
echo $b = false && $a==1 //false

//逻辑与 && > 逻辑或 || > 赋值 > and > or 

echo $a = false || true;  // true
echo $b = false or true // true
```

实例：

下列程序中请写出打印输出的结果：

```php
$a = 0;
$b = 0;

if ($a = 3 > 0 || $b = 3 > 0) 
{
    $a++;
    $b++;
    echo $a. "\n";
    echo $b. "\n";
}
```

结果：

```
boolean true
int 0
1 1
```

解析：

1.优先级 ：比较运算符（>） `||` > 赋值运算

2.按照以上逻辑， `3 > 0 为true，继续查看为逻辑或，逻辑或的原则是i前为true，则逻辑结果即可返回true，后面不在执行`

3.因此$a 拿到了逻辑运算后的true，而$b没有执行，仍然为0

4.前面说过对一个boolean类型的值进行递增/递减不会影响布尔值 ,因此

* $a的值不发生改变，输出被强制转为1.
* $b=0;$b++;则为1

## 拓展

* 判断int型变量a是奇数还是偶数：a&1 = 0 偶数  a&1 = 1 奇数
* 乘法运算转化成位运算：a \* (2^n) 等价于 a<< n
* 除法运算转化成位运算：a / (2^n) 等价于 a>> n
* 不用temp交换两个整数x ^= y; y ^= x; x ^= y;
* 二进制位掩码，提供了一种用一个选项表示多项的可能。（参考：error\_reporting设置错误级别的方式）

```php
Error_Reporting(E_Error | e_Warning | e_Parse | e_Notice);
error_reporting(E_ALL ^ E_NOTICE);
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://phper.shujuwajue.com/ji-chu/yun-suan-fu-yu-cuo-wu-kong-zhi-7b26.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
