Home  >  Article  >  Backend Development  >  Introduction to various operators in php and examples

Introduction to various operators in php and examples

伊谢尔伦
伊谢尔伦Original
2017-06-21 09:27:191875browse

An

operator is something that takes one or more given values ​​(an expression, in programming jargon) and produces another value (thus the entire structure becomes an expression).

1. Arithmetic operators

Operator Name Result
$a + $b Addition Sum of $a and $b
$a - $b Subtraction The difference between $a and $b
$a * $b Multiplication The product of $a and $b
$a / $b Division The quotient of $a divided by $b
$a % $b Modulo The remainder of dividing $a by $b

2. Increment/Decrease Operator
Operator Name Result
++$a Before Add the value of $a plus one, and then perform the operation
$a++ Then add the value of $a first, perform the operation first, then add one
--$a Subtract the value of $a before subtraction by one, and then perform the operation
$a-- The value of $a is operated first, then decremented by one
Example:

<?php 
  echo  $a=5+”5th”;    //输出:10 
  echo  10%3;          //输出:1 
  echo  10+ $a++;      //输出:20 
  echo   5- --$a;        //输出:-5 
?>

3. Comparison operator
Operator Name Result
$a == $b is equal to TRUE if $a is equal to $b
$a === $b is congruent TRUE if $a is equal to $b and they are of the same type
$a != $b No etc. TRUE if $a is not equal to $b
$a a8093152e673feb7aba1828c43532094 $b is not equal TRUE if $a is not equal to $b
$a !== $b is not congruent TRUE if $a Not equal to $b, or they are of different types
$a 57fda13365877b4c578b2cdbbc0adb3d $b is greater than TRUE if $a is strictly $b
$a b25c5a3e7436edc83de0e458d4b26e56= $b Greater than or equal to TRUE if $a is greater than or equal to $b
Another conditional operator is " ? : " (or ternary) operator.
Example:

<?php 
     var_dump(0=="a");  //输出:bool(true)       
    var_dump(0=="00");      //输出:bool(true) 
    var_dump(0==="00");    //输出:bool(false) 
    var_dump(0<>"abc");   //输出:bool(false) 
    var_dump(0!=="01");  //输出:bool(true)  
    $a=10; 
    $b=20; 
    $str=$a>$b? "true":"false";  
    echo $str;    //输出:false  
?>

4. 逻辑运算符 
  运算符      名称      结果 
  $a and $b    逻辑与    TRUE,如果 $a 与 $b 都为 TRUE。 
  $a or $b    逻辑或    TRUE,如果 $a 或 $b 任一为TRUE。 
  $a xor $b    异或        TRUE,如果 $a 和 $b 不同时 
  ! $a              逻辑非  TRUE,如果 $a 不为 TRUE。 
  $a && $b      逻辑与  TRUE,如果 $a 与 $b 都为TRUE。 
  $a || $b        逻辑或  TRUE,如果 $a 或 $b 任一为TRUE。 
  其中and与&& 、or与||是同一逻辑运算符的两种写法。  
  逻辑与和逻辑或 都是短路运算符。

在遇到下列逻辑表达式时,PHP解释程序将不会计算右边的表达式: 

  <?php 
      $a=10; 
      if(false && (++$a)); 
      echo $a;      //输出:10 
      $b=10; 
      if(true or (++$b)); 
      echo $b;      //输出:10 
  ?>

5. 位运算符 
  位运算符允许对整型数中指定的位进行置位。如果左右参数都是字符串,则位运算符将操作字符的 ASCII 值。 
  表达式     名称      结果 
  $a & $b   按位与     将把 $a 和 $b 中都为 1 的位设为 1。 
  $a | $b     按位或    将把 $a 或者 $b 中为 1 的位设为 1。 
  $a ^ $b   按位异或  将把 $a 和 $b 中不同的位设为 1。 
  ~ $a       按位非    将 $a 中为 0 的位设为 1,反之亦然。 
  $a 4199223a77a3e66d1c0cfe54d715f43c> $b   右移      将 $a 中的位向右移动 $b 次(每一次                           移动都表示“除以 2”)。 

 其他运算符 
1. 字符串运算符 
有两个字符串运算符。第一个是连接运算符(“.”),它返回其左右参数连接后的字符串。第二个是连接赋值运算符(“.=”),它将右边参数附加到左边的参数后。 
错误抑制操作符 
在最常见的数据库连接与文件创建操作或出现除0等异常时,可以用@符号来抑制函数错误信息输出到浏览器端 $a=@(5/0) 
外部命令执行 
 使用``来运行外部系统命令,注意不是单引号,是ESC下面那个按键        

<?php 
  $out=`dir c:`; 
  print_r($out); 
?>
//不建议使用

实例: 

<?php 
  $a="hello"; 
  $a.=" world! ";  //等同于:$a=$a." world!"; 
  echo $a;  //输出:hello world!   
  $m = 3; 
  $m += 5;  //等同于:$m=$m+5; 
  echo $m;   //输出:8 
  $c = ($b = 4) + 5;  
  echo $c;  //输出:9   
?>

2. 运算符优先级  
下表从低到高列出了运算符的优先级。  
  结合方向    运算符 
  左      , 
  左      or 
  左      xor 
  左      and 
  右      print 
  右      = += -= *= /= .= %= &= |= ^=                      ~=   341666e55d71dd76c0b6e313fcc8374e>=  
  左      ? : 
  左      || 
  左      && 
结合方向        运算符 
  左      | 
  左      ^ 
  左      & 
  无      == != === !== 
  无      6e256fa4b1026ecd9077150abca744f9 >=  
  左      4dc0da33faa5301e003c483fcf957627> 
  左      +  -  . 
  左      *  /   % 
  右      ! ~ ++ -- (int) (float) (string) (array) (object) @ 
  右      [ 
  无      new

The above is the detailed content of Introduction to various operators in php and examples. For more information, please follow other related articles on the PHP Chinese website!

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