PHP operatorsLOGIN

PHP operators

PHP Arithmetic Operator

## + - *
## Operator Name Example Result
Addition$x + $ySum of $x and $y
Subtraction$ x - $yThe difference between $x and $y
Multiplication$x * $yThe product of $x and $y
/ Division$x / $yQuotient 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

## Concatenation$txt1 = "Hello" $txt2 = $txt1 . " world!" Now $txt2 contains "Hello world!" Concatenation assignment $txt1 = "Hello" $txt1 .= " world!"Now $txt1 contains "Hello world!"

Example

The following example shows the result of using string operators:

<?php
 $a = "Hello";
 $b = $a . " world!";
 echo $b; // 输出 Hello world!
 echo "<br/>";
 $x="Hello";
 $x .= " world!";
 echo $x; // 输出 Hello world!
 ?>

PHP increment/decrement operator

Increment and decrement are simply to add one to yourself Or minus one

Operator Name Example Result
.
.=
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):

## == Equal to $x == $y Returns true if $x is equal to $y. If $x is equal to $y and they are of the same type, return true != is not equal to $x != $yReturns true if $x is not equal to $y.
Operator Name Example Result
===
Congruent (identical)
$x === $y
<> Not equal to$x <> $yIf $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 > $yReturns true if $x is greater than $y.
< is less than $x < $yReturns true if $x is less than $y.
>= Greater than or equal to$x >= $yIf $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



and and$x and $yIf $x and $y are both true, return true. ## or or$x or $y## xor

Operator Name Example Result



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 && $yIf If both $x and $y are true, return true. ! Not!$xIf $x is not true, returns true.

Let’s give a few examples to try,

Logical AND:

<?php
 header("Content-type:text/html;charset=utf-8");    //设置编码
 $x = true;
 $y = false;
 //逻辑与(并且),要求两个都为true才执行真区间,所以代码中执行假区间
 if($x && $y){
     echo '执行了真区间';
 }else{
     echo '执行了假区间';
 }
 ?>

Logical OR:

<?php
 header("Content-type:text/html;charset=utf-8");    //设置编码
 $foo = false;
 $bar = true;
 //逻辑或,有一个为真则为真
 if($foo || $bar){
     echo '执行真区间';
 }else{
     echo '执行假区间';
 }
 
 ?>

Logical NOT:

<?php
 header("Content-type:text/html;charset=utf-8");    //设置编码
 $foo = false;
 //逻辑非,把false变为了true
 if(!$foo){
     echo '执行真区间';
 }else{
     echo '执行假区间';
 }
 
 ?>

PHP array operator


## ||

## or

$x || $y

Returns true if at least one of $x and $y is true.

## + == Equal$x == $y## ===
## Operator Name Example Result
Union$x + $y Union of $x and $y (but not Overwrite duplicate keys)



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

Congruent

$x === $y
If $x and $y have the same key/value pairs in the same order and type, return true.
!= Not equal$x != $yIf $x is not equal to $y, return true.
<>Not equal$x <> $yIf $x is not equal to $ y, returns true.
!==Not congruent$x !== $yIf $x is completely different from $y , then returns true.

PHP array operators are used to compare arrays:

Example

The following example shows different results using different array operators:

<?php
 $x = array("a" => "red", "b" => "green");
 $y = array("c" => "blue", "d" => "yellow");
 $z = $x + $y; // $x 与 $y 的联合
 var_dump($z);
 var_dump($x == $y);
 var_dump($x === $y);
 var_dump($x != $y);
 var_dump($x <> $y);
 var_dump($x !== $y);
 ?>

Ternary operator


##Ternary operator format:

##(expr1)?(expr2):(expr3); //Expression1?Expression2:Expression3

Example

<?PHP
 $a=10; $b=20;
 $c=$a>$b?($a-$b):($a+$b);
 //说明:如果变量a大于变量b则执行问号后面的,否则就执行:冒号后面的
 echo $c;
 ?>


Next Section

<?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 ?>
submitReset Code
ChapterCourseware