PHP Basics Operators

黄舟
Release: 2023-03-05 12:22:01
Original
1709 people have browsed it

1 Definition
An operator is something that can be given one or more values ​​(in programming jargon, an expression) to produce another value (thus the entire structure becomes an expression).

Operators can be grouped according to how many values ​​they can accept:
① Unary operators can only accept one value, such as ! (logical negation operator) or ++ (increment operator).
② Binary operators accept two values, such as the familiar arithmetic operators + (addition) and - (subtraction), which are the majority of PHP operators.
③ Ternary operator ? :, accepts three values; usually simply called the "ternary operator" (although it may be more appropriate to call it a conditional operator).

2 Operator priority
① Operator priority specifies how "closely" two expressions are bound. For example, the expression 1 + 5 * 3 evaluates to 16 instead of 18 because the multiplication sign ("*") has higher precedence than the plus sign ("+").
② If necessary, parentheses can be used to force the priority to change. For example: (1 + 5) * 3 has a value of 18.
③ If the operators have the same precedence, the combination direction of the operators determines how to operate. For example, "-" is a left-joint, then 1 - 2 - 3 is equivalent to (1 - 2) - 3 and the result is -4.
④ "=" is a right-joint, so $a = $b = $c is equivalent to $a = ($b = $c).
⑤ Operators with the same priority that are not combined cannot be used together. For example, 1 < 2 > 1 is illegal in PHP. But on the other hand the expression 1 <= 1 == 1 is legal because == has a lower precedence than <=.
⑥ The use of brackets, even when it is not necessary, clearly indicates the order of operations through the pairing of brackets, rather than relying on operator priority and associativity, which can usually increase the readability of the code.

3 Arithmetic operators
① Negation For example: -$a represents the negative value of $a.
② Addition like: $a + $b
③ Subtraction like: $a - $b
④ Multiplication like: $a * $b
⑤ Division like: $a / $b
⑥ Modulo Such as: $a % $b
⑦ Exponentiation such as: $a ** $b

Note:

a. The division operator always returns a floating point number. The only exception is that both operands are integers (or integers converted from strings) and are exactly divisible, in which case it returns an integer.

b. The operands of the modulo operator will be converted to integers (except for the decimal part) before operation.

c. The result of the modulo operator % is the same as the sign (sign) of the dividend. That is, the result of $a % $b has the same sign as $a

4 Assignment operator
① The basic assignment operator is "=". At first you may think it is "equal to", but it is not. It actually means assigning the value of the expression on the right to the operand on the left.
The value of the assignment expression is the assigned value. That is, the value of "$a = 3" is 3. In this way, you can do some tricks:

 <?php
    $a = ($b = 4) + 5; // $a 现在成了 9,而 $b 成了 4。
    ?>
Copy after login

② Binary arithmetic: the "combination operator" of array collection and string operators, so that its value can be used in an expression and the expression The result is assigned to it

<?php
$a = 3;
$a += 5; // sets $a to 8, as if we had said: $a = $a + 5;
$b = "Hello ";
$b .= "There!"; // sets $b to "Hello There!", just like $b = $b . "There!";
?>
Copy after login

③ Reference assignment: PHP supports reference assignment, using the "$var = &$othervar;" syntax. Assignment by reference means that both variables point to the same data, nothing is copied.

<?php
$a = 3;
$b = &$a; // $b 是 $a 的引用

print "$a\n";
 // 输出 3
print "$b\n"; // 输出 3

$a = 4; // 修改 $a

print "$a\n"; // 输出 4
print "$b\n"; // 也输出 4,因为 $b 是 $a 的引用,因此也被改变
?>
Copy after login

④ Common sense
The assignment operation copies the value of the original variable to the new variable (pass-by-value assignment), so changing one does not affect the other. This is also suitable for copying some values ​​such as large arrays in dense loops.

5 位运算符
① And(按位与) $a & $b
② Or(按位或) $a | $b
③ Xor(按位异或) $a ^ $b
④ Not(按位取反) ~ $a
⑤ Shift left(左移) $a << $b
⑥ $a >> $b

6 比较运算符
① 等于 $a == $b
② 全等 $a === $b
③ 不等 $a != $b
④ 不等 $a <> $b
⑤ 不全等 $a !== $b
⑥ 小于 $a < $b
⑦ 大于 $a > $b
⑧ 小于等于 $a <= $b
⑨ 大于等于 $a >= $b
⑩ 结合比较运算符 $a <=> $b

7 错误控制运算符
PHP 支持一个错误控制运算符:@。当将其放置在一个 PHP 表达式之前,该表达式可能产生的任何错误信息都被忽略掉。
如果用 set_error_handler() 设定了自定义的错误处理函数,仍然会被调用,但是此错误处理函数可以(并且也应该)调用 error_reporting(),而该函数在出错语句前有 @ 时将返回 0。
如果激活了 track_errors 特性,表达式所产生的任何错误信息都被存放在变量 $php_errormsg 中。此变量在每次出错时都会被覆盖,所以如果想用它的话就要尽早检查。

8 执行运算符
PHP 支持一个执行运算符:反引号(``)。注意这不是单引号!PHP 将尝试将反引号中的内容作为 shell 命令来执行,并将其输出信息返回(即,可以赋给一个变量而不是简单地丢弃到标准输出)。使用反引号运算符“`”的效果与函数 shell_exec() 相同。

<?php
$output = `ls -al`;
echo "<pre class="brush:php;toolbar:false">$output
"; ?>
Copy after login

注:反引号运算符在激活了安全模式或者关闭了 shell_exec() 时是无效的。

9 递增/递减运算符: PHP 支持 C 风格的前/后递增与递减运算符。
① 前加 ++$a
② 后加 $a++
③ 前减 --$a
④ 后减 $a--

10 逻辑运算符
① And(逻辑与) $a and $b
② Or(逻辑或) $a or $b
③ Xor(逻辑异或) $a xor $b
④ Not(逻辑非) ! $a
⑤ And(逻辑与) $a && $b
⑥ Or(逻辑或) $a || $b

11 字符串运算符
有两个字符串(string)运算符。第一个是连接运算符(“.”),它返回其左右参数连接后的字符串。第二个是连接赋值运算符(“.=”),它将右边参数附加到左边的参数之后。更多信息见赋值运算符。

<?php
$a = "Hello ";
$b = $a . "World!"; // now $b contains "Hello World!"
$a = "Hello ";
$a .= "World!";     // now $a contains "Hello World!"
?>
Copy after login

12 数组运算符
① 联合 $a 和 $b 的联合。 $a + $b
② 相等 如果 $a 和 $b 具有相同的键/值对则为 TRUE。 $a == $b
③ 全等 如果 $a 和 $b 具有相同的键/值对并且顺序和类型都相同则为 TRUE。 $a === $b
④ 不等 如果 $a 不等于 $b 则为 TRUE。 $a != $b
⑤ 不等 如果 $a 不等于 $b 则为 TRUE。 $a <> $b
⑥ 不全等 如果 $a 不全等于 $b 则为 TRUE。 $a !== $b

注:+ 运算符把右边的数组元素附加到左边的数组后面,两个数组中都有的键名,则只用左边数组中的,右边的被忽略。

13 类型运算符
instanceof 用于确定一个 PHP 变量是否属于某一类 class 的实例:

<?php
class MyClass
{
}

class NotMyClass
{
}
$a = new MyClass;

var_dump($a instanceof MyClass);
var_dump($a instanceof NotMyClass);
?>
Copy after login

以上就是PHP基础 之 运算符的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!