Home  >  Article  >  Backend Development  >  PHP Basics Operators

PHP Basics Operators

黄舟
黄舟Original
2017-02-06 09:54:371764browse

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 d66d2a93259ca9392db29f00ed43fda0 1 is illegal in PHP. But on the other hand the expression 1 ff37a356291e3365203dcfe20f1f7a14> $b
   
6 比较运算符
  ① 等于 $a == $b
  ② 全等 $a === $b
  ③ 不等 $a != $b
  ④ 不等 $a a8093152e673feb7aba1828c43532094 $b
  ⑤ 不全等 $a !== $b
  ⑥ 小于 $a b1dfc4fa0989d7b44e7884352ac14384 $b
  ⑧ 小于等于 $a 9c9c9711494fc347148f429f658f4d1e= $b
  ⑩ 结合比较运算符 $a 96b4fef55684b9312718d5de63fb7121 $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
"; ?>

  注:反引号运算符在激活了安全模式或者关闭了 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!"
?>

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

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


Statement:
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