PHP operators
PHP Arithmetic Operator
## Operator | Name | Example | Result | ||||||||||||
Addition | $x + $y | Sum of $x and $y | |||||||||||||
Subtraction | $ x - $y | The difference between $x and $y | |||||||||||||
Multiplication | $x * $y | The product of $x and $y |
/ | Division | $x / $y | Quotient of $x and $y |
% | Remainder is also called modulus and modulus | $x % $y | $x is the remainder after dividing $y |
Example
The following example shows different results using different arithmetic operators:
<?php $x=10; $y=6; echo ($x + $y)."<br/>"; // 输出 16 echo ($x - $y)."<br/>"; // 输出 4 echo ($x * $y)."<br/>"; // 输出 60 echo ($x / $y)."<br/>"; // 输出 1.6666666666667 echo ($x % $y)."<br/>"; // 输出 4 ?>
PHP assignment operator
In mathematics, we call = (an equal sign) the assignment operator, that is: assign the value on the right side of the equal sign. If the variable on the left side of the equal sign is given, the variable on the left side will be the value on the right side.
Symbol | Example | Equivalent equation |
+= | $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 |
.= | $x .= $y | #$x = $x . $y |
Examples
The following examples and equivalent equations are clearly explained.
$x += $y is equivalent to $x = $x + $y
<?php $x = 5; $y = 8; $x += $y; echo $x;
PHP characters String operator
Operator | Name | Example | Result | ||||||||||||
. | ## Concatenation$txt1 = "Hello" $txt2 = $txt1 . " world!" | Now $txt2 contains "Hello world!" | |||||||||||||
.= | Concatenation assignment$txt1 = "Hello" $txt1 .= " world!" | Now $txt1 contains "Hello world!" |
Symbol | Description |
$x++ | Assign first and then add |
$x-- | Assign first and then subtract |
++$x | Add first and then assign value |
--$x | Subtract first and then assign value |
The above usage is actually quite simple, follow the above example. We divide it into steps and judge according to the process.
Example
<?php $x = 5; //先赋值后加:即先将$x的值赋值给$y。$x的值为5,所以将$x的值赋值给$y。$y也为5 $y = $x++; //$x的结果输出为6,因为赋值给$y后,$x自己又把自己进行了+1操作。所以,$x的结果为6 echo $x; ?>
Let’s compare adding first and then assigning, as follows:
<?php $x = 8; $y = ++$x; echo $x; ?>
You can do another experiment and try $x-- and--$x
## Small test
See if you can guess the result of $water + $paper below?
<?php $x = 5; $y = 6; $paper = ++$x + $x++; $water = $y-- + $x--; echo $water + $paper; ?>
Did you guess it right
# #PHP comparison operators:
PHP comparison operators are used to compare two values (numbers or strings):
Operator | Name | Example | Result | ||||||||||||
Equal to | $x == $y | Returns true if $x is equal to $y. | |||||||||||||
=== | Congruent (identical) | $x === $y | If $x is equal to $y and they are of the same type, return true|||||||||||||
is not equal to | $x != $y | Returns true if $x is not equal to $y. |
<> | Not equal to | $x <> $y | If $x is not equal to $ y, returns true. |
!== | Not congruent (completely different) | $x !== $y | If $x is not equal to $y, and they are not the same type, return true. |
> | is greater than | $x > $y | Returns true if $x is greater than $y. |
< | is less than | $x < $y | Returns true if $x is less than $y. |
>= | Greater than or equal to | $x >= $y | If $x is greater than or equal to $ y, then return true. |
<= | is less than or equal to | $x <= $y | Returns true if $x is less than or equal to $y. |
Example
The following example shows different results using certain comparison operators:
<?php $x=100; $y="100"; var_dump($x == $y); echo "<br>"; var_dump($x === $y); echo "<br>"; var_dump($x != $y); echo "<br>"; var_dump($x !== $y); echo "<br>"; $a=50; $b=90; var_dump($a > $b); echo "<br>"; var_dump($a < $b); ?>
##PHP Logical Operator
Operator | Name | Example | Result | ||||||||||||
and | $x and $y | If $x and $y are both true, return true. | |||||||||||||
if $x and If at least one of $y is true, return true. | |||||||||||||||
XOR | $x xor $y | if $x If only one of $y and $y is true, then true is returned. |
and | $x && $y | If If both $x and $y are true, return true. | |
| ## or | $x || $y Returns true if at least one of $x and $y is true. | ! |
!$x | If $x is not true, returns true. |