Detailed explanation of PHP operators

Here is a summary of PHP operators

 

【List】

① Arithmetic operators;

② assignment operator;

③ string operator;

④ Increment/decrement operator;

⑤Comparison operator;

⑥ Logical operators;

⑦Array operator;

 

【Details】

   ① Arithmetic operators: +, -, *, /, % (addition, subtraction, multiplication and division remainder)

   ②Assignment operator: The PHP assignment operator is used to write values ​​to variables. The basic assignment operator is "=", which means that the assignment expression on the right will set the value of the operand on the left. List:

    x = y x = y

   x += y x = x + y

   x -= y x = x - y

   x *= y x = x * y

   x /= y x = x / y

   x %= y x = x % y

   ③ String operators: concatenation, concatenation assignment

. tandem $txt1 = "Hello" $txt2 = $txt1 . " world!" $txt2 now contains "Hello world!"
.= concatenation assignment $txt1 = "Hello" $txt1 .= " world!" $txt1 now contains "Hello world!"

   Case:

<?php
$a = "Hello";
$b = $a . "world!";//Concatenation
echo $b; // print Hello world!
$x="Hello";
$x .= "world!";//Concatenate assignment 1
echo $x; // output Hello world!
?>

   ④ Increment/Decrement operator

++$x pre-increment Increment $x by one, then return $x
$x++ post increment Returns $x, then increments $x by one
--$x pre-decrement Decrement $x by one, then return $x
$x-- post decrement Return $x, then decrement $x by one

   Case:

<?php
$x=10;
echo ++$x; // output 11
$y=10;
echo $y++; // output 10
$ z = 5;
echo --$z; // output 4
$i=5;
echo $i--; // output 5
?>

    ⑤Comparison operator

== equals $x == $y Returns true if $x equals $y.

=== Congruent (identical) $x === $y Returns true if $x is equal to $y and they are of the same type.

!= not equal to $x != $y Returns true if $x is not equal to $y.

<> not equal to $x <> $y Returns true if $x is not equal to $y.

!== not congruent (completely different) $x !== $y Returns true if $x is not equal to $y, or if they are not of the same type

> greater than $x > $y Returns true if $x is greater than $y.

< less than $x < $y Returns true if $x is less than $y.

>= greater than or equal to $x >= $y Returns true if $x is greater than or equal to $y.

<= less than or equal to $x <= $y Returns true if $x is less than or equal to $y.

    ⑥ Logical operators

and           与                  $x and $y

or           或             $x or $y

xor XOR $x xor $y

&&           与                  $x && $y

|| or $x || $y

! not !$x

     ⑦Array operator

Array operators are used to compare arrays

+ union of $x and $y (but not covering duplicate keys)

== equal Returns true if $x and $y have the same key/value pair.

=== Congruent Returns true if $x and $y have the same key/value pair, same order, same type.

!= not equal Returns true if $x is not equal to $y.

<> not equal Returns true if $x is not equal to $y.

!== Not Congruent Returns true if $x and $y are completely different.

   Example:

<?php
$x = array("a" => "red", "b" => "green");
$y = array("c" => "blue", "d" => "yellow");
$z = $x + $y; // union of $x and $y
var_dump($z);
var_dump($x == $y);
var_dump($x === $y);
var_dump($x != $y);
var_dump($x <> $y);
var_dump($x !== $y);
?>

 

 

 

 

 

 

 

 

 

.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326220580&siteId=291194637