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:

"; // 输出 16 echo ($x - $y)."
"; // 输出 4 echo ($x * $y)."
"; // 输出 60 echo ($x / $y)."
"; // 输出 1.6666666666667 echo ($x % $y)."
"; // 输出 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 characters String operator

Example

The following example shows the result of using string operators:

"; $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

## Concatenation $txt1 = "Hello" $txt2 = $txt1 . " world!" Now $txt2 contains "Hello world!" Concatenation assignment $txt1 = "Hello" $txt1 .= " world!" Now $txt1 contains "Hello world!"
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

Let’s compare adding first and then assigning, as follows:

You can do another experiment and try $x-- and--$x


## Small test

See if you can guess the result of $water + $paper below?

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 != $y Returns true if $x is not equal to $y.
Operator Name Example Result
===
Congruent (identical)
$x === $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:

"; var_dump($x === $y); echo "
"; var_dump($x != $y); echo "
"; var_dump($x !== $y); echo "
"; $a=50; $b=90; var_dump($a > $b); echo "
"; var_dump($a < $b); ?>

##PHP Logical Operator



and and $x and $y If $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.

Let’s give a few examples to try,

Logical AND:

Logical OR:

Logical NOT:

PHP array operator

## && and $x && $y If If both $x and $y are true, return true. ! Not !$x If $x is not true, returns true.


## ||

## 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 != $y If $x is not equal to $y, return true.
<> Not equal $x <> $y If $x is not equal to $ y, returns true.
!== Not congruent $x !== $y If $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:

 "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

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


Continuing Learning
||
"; // 输出 16 echo ($x - $y)."
"; // 输出 4 echo ($x * $y)."
"; // 输出 60 echo ($x / $y)."
"; // 输出 1.6666666666667 echo ($x % $y)."
"; // 输出 4 ?>
submit Reset Code
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!