Home  >  Article  >  Backend Development  >  Take you two minutes to understand the operators in PHP

Take you two minutes to understand the operators in PHP

烟雨青岚
烟雨青岚forward
2020-07-10 13:16:151986browse

Take you two minutes to understand the operators in PHP

(1) Arithmetic operator

Take you two minutes to understand the operators in PHP

<?php 
     $maxLine = 4; //每排人数
     $no = 17;//学生编号
     $line = ceil($no/$maxLine); // 向上取整
     $row = $no%$maxLine ? $no%$maxLine : $maxLine;    

     echo "编号<b>".$no."</b>的座位在第<b>".$line."</b>排第<b>".$row."</b>个位置";?>

(2) Assignment operator

  • "=": Assign the value of the expression on the right to the operand on the left. It copies the value of the expression on the right and gives it to the operand on the left. In other words, first apply for a piece of memory for the operand on the left, and then put the copied value into this memory
  • "&": reference assignment, which means that both variables point to the same data. It will cause two variables to share a piece of memory. If the data stored in this memory changes, the values ​​​​of both variables will change
<?php 
    $a = "我在慕课网学习PHP!"; 
       $b = $a;    $c = &$a; 
       $a = "我天天在慕课网学习PHP!";
        echo $b."<br />"; 
     // 我在慕课网学习PHP!
    echo $c."<br />"; 
     // 我天天在慕课网学习PHP!
     ?>

(3) Comparison operator

Take you two minutes to understand the operators in PHP

<?php  
    $a = 1;    $b = "1";
    var_dump($a == $b); // true

    var_dump($a === $b); // false

    var_dump($a != $b); //false

    var_dump($a <> $b); // false

    var_dump($a !== $b); // true

    var_dump($a < $b); //false

    $c = 5;
    var_dump($a < $c); //true

    var_dump($a > $c); // false

    var_dump($a <= $c); // true

    var_dump($a >= $c); // false

    var_dump($a >= $b); // true?>

(4) Ternary operator

  • ("?:") The ternary operator is also a Comparison operator
  • Expression (expr1)?(expr2):(expr3), if the value of expr1 is true, the value of this expression is expr2, otherwise it is expr3.
<?php 
    $a = 78;//成绩
    $b = $a >= 60 ? "及格": "不及格"; 
    echo $b;?>

(5) Logical operator

Take you two minutes to understand the operators in PHP

(6) String linker

  • Concatenation operator ("."): It returns the string obtained by appending the right parameter to the left parameter
  • Concatenation assignment operator (".="): It Append the right parameter to the left parameter
<?php 
    $a = "张先生";    $tip = $a.",欢迎您在慕课网学习PHP!";
        $b = "东边日出西边雨";    
    $b .= ",道是无晴却有晴";
        $c = "东边日出西边雨";    
    $c = $c.",道是无晴却有晴";
        echo  $tip."<br />";
            echo  $b."<br />"; 
               echo  $c."<br />";
               ?>

(7) Error control operator

  • An error control operator is provided in PHP "@", for some expressions that may cause errors during operation, we do not want to display error messages to customers when errors occur, which is not user-friendly.
  • You can place @ in a PHP expression Previously, any error messages that might be generated by the expression were ignored
  • If the track_error (this thing is set in php.ini) feature is activated, any error messages generated by the expression are stored in variables In $php_errormsg, this variable will be overwritten every time an error occurs, so if you want to use it, you must check it as soon as possible
  • It should be noted that: the error control prefix "@" will not block parsing error information, and cannot Put it before the definition of a function or class, and it cannot be used for conditional structures such as if and foreach.
<?php  
 $conn = @mysql_connect("localhost","username","password");
  echo "出错了,错误原因是:".$php_errormsg;
  ?>

Thank you for reading, I hope you will benefit a lot.

This article is reproduced from: https://blog.csdn.net/sinat_35615296/article/details/78813100

Recommended tutorial: "php tutorial"

The above is the detailed content of Take you two minutes to understand the operators in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete