Take you two minutes to understand the operators in PHP

烟雨青岚
Release: 2023-04-09 07:16:02
forward
1935 people have browsed it

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>个位置";?>
Copy after login

(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!
     ?>
Copy after login

(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?>
Copy after login

(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;?>
Copy after login

(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 />";
               ?>
Copy after login

(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;
  ?>
Copy after login

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!

Related labels:
php
source:csdn.net
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!