Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

PhP operator


May 10, 2021 PHP


Table of contents


PhP operator

Operators in PHP are divided into arithmetic operators, increment/decrease operators, comparison operators, logical operators, array operators, omnomary operators, and so on.

In this section we will discuss the application of different operators in PHP.

In PHP, the assignment operator is used to assign values to variables.

In PHP, the arithmetic operator, plus, is used to add values together.


PHP arithmetic operator

Operator Name Describe Instance Results
x + y Add The same as x and y 2 + 2 4
x - y Reducing The difference between x and y 5 - 2 3
x * y By The product of x and y 5 * 2 10
x / y Except the business of x and y 15 / 5 3
x % y Mode (residuals of the divider) x divided by the remaining number of y 5 % 2
10 % 8
10 % 2
1
2
0
- x Take the opposite x Reverse - 2
A. B It's in the same place Connect two strings "Hi" . "Ha" HiHa

The following example demonstrates the different results obtained using different arithmetic operators:

<?php
$x=10;
$y=6;
echo ($x + $y); O utput 16
echo ($x - $y); O utput 4
echo ($x * $y); O utput 60
echo ($x / $y); O utput 1.666666666667
echo ($x % $y); O utput 4
?>

Try it out . . .

PHP assignment operator

In PHP, the basic assignment operator is "" I t means that the left operans are set to the value of the expression on the right. That is, $x value of "5" is 5.

Operator Equivalent to Describe
x = y x = y The left operanex is set to the value of the expression on the right
x += y x = x + y Add
x -= y x = x - y Reducing
x *= y x = x * y By
x /= y x = x / y Except
x %= y x = x % y Mode (residuals of the divider)
a .= b a = a . B Connect two strings

The following example demonstrates the different results obtained using different assignment operators:

<?php
$x=10;
echo $x; O utput 10

$y=20;
$y += 100;
echo $y; O utput 120

$z=50;
$z -= 25;
echo $z; O utput 25

$i=5;
$i *= 6;
echo $i; O utput 30

$j=10;
$j /= 5;
echo $j; O utput 2

$k=15;
$k %= 4;
echo $k; O utput 3
?>

Try it out . . .

The following example demonstrates the same results with different string operators:

<?php
$a = "Hello";
$b = $a . " world!";
echo $b; O utput Hello world!

$x="Hello";
$x .= " world!";
echo $x; O utput Hello world!
?>

Try it out . . .

PHP increment/decrease operator

Operator Name Describe
++ x Pre-increment x plus 1, and then return x
x ++ after increment Return x, and then x plus 1
-- x Pre-decrease x minus 1, and then return x
x -- after decreasing Return x, and then x minus 1

The following example demonstrates the results obtained using the increment/decrease operator:

<?php
$x=10;
echo ++$x; O utput 11

$y=10;
echo $y++; O utput 10

$z=5;
echo --$z; O utput 4

$i=5;
echo $i--; O utput 5
?>

Try it out . . .

PHP comparison operator

The comparison operator allows you to compare two values:

Operator Name Describe Instance
x == y Equals If x is equal to y, true is returned 5-8 returns false
x === y Constant equals True is returned if x is equal to y and they are of the same type "5" returns false
x != y Not equal to True is returned if x is not equal to y 5!-8 returns true
x <> y Not equal to True is returned if x is not equal to y 5-lt;8 returns true
x !== y Not constant equal to True is returned if x is not equal to y, or if they are not of the same type 5! "5" returns true
x > y Greater than If x is greater than y, true is returned 5>8 returns false
x < y Less than If x is less than y, true is returned 5-lt;8 returns true
x >= y Is greater than or equal to True is returned if x is greater than or equal to y 5 sgt; s8 return false
x <= y Less than or equal to True is returned if x is less than or equal to y 5-lt;=8 returns true

The following example demonstrates the different results obtained using some 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);
?>

Try it out . . .

PHP logical operator

Operator Name Describe Instance
x and y And True is returned if both x and y are true x=6
y=3
(x slt; 10 and y and 1) return true
x or y Or If x and y have at least one true, true is returned x=6
y=3
(x-6 or y-5) returns true
x xor y different or not True is returned if x and y have and only one is true x=6
y=3
(x-6 xor y-3) returns false
x && y And True is returned if both x and y are true x=6
y=3
(x slt; 10 and y and y; 1) return true
x || Y Or If x and y have at least one true, true is returned x=6
y=3
(x-5|| y-5) returns false
! Non - If x is not true, true is returned x=6
y=3
! (x-y) returns true

PHP array operator

Operator Name Describe
x + y Collection A collection of x and y
x == y Equal True is returned if x and y have the same key/value pairs
x === y Identical True is returned if x and y have the same key/value pairs and the order is the same
x != y Not equal True is returned if x is not equal to y
x <> y Not equal True is returned if x is not equal to y
x !== y Not constant True is returned if x is not equal to y

The following example demonstrates the different results obtained using some array operators:

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

Try it out . . .

PHP thyme operator

Another conditional operator is the ?: (or thymus) operator.

Grammar format:

(expr1) ? (expr2) : (expr3)

The value for expr1 is expr2 when true and expr3 when expr1 is false.

Starting with PHP 5.3, the middle part of the 3-function operator can be omitted. Expression expr1 ?: expr3 returns expr1 when expr1 is evaluated as TRUE, otherwise expr3 is returned.

Instance

In the following example, by judging that the $_GET request contains a user value, if there is a return of $_GET'user', otherwise nobody is returned:

<?php
$test s'w3cschool online tutorial';
Common handwriting
$username = isset($test) ? $ test : 'nobody'; e cho $username, PHP_EOL;

PHP version writing 5.3 plus
$username = $test ?: 'nobody'; e cho $username, PHP_EOL;
?>

Operator priority

Operators prioritize the grouping of terms in an expression. T his affects how expressions are evaluated. S ome operators take precedence over others;

The following table lists operators from highest to lowest by priority. Operators in the same row have the same priority, and their binding direction determines the order of the calculations.

Combine directions Operator Additional information
No clone new clone and new
Left to right
[ array()
Right to left
++ -- ~ (int) (float) (string) (array) (object) (bool) @ Type and increment/decrease
No instanceof Type
Right to left
! The logical operator
Left to right
*  /  % Arithmetic operator
Left to right
+ – . Arithmetic operators and string operators
Left to right
<< >> Bit operator
No ==  !=  ===  !==  <> Comparison operator
Left to right
& Bit operators and references
Left to right
^ Bit operator
Left to right
| Bit operator
Left to right
&& The logical operator
Left to right
|| The logical operator
Left to right
? : The thyme operator
Right to left
=  +=  -=  *=  /=  .=  %=  &=  |=  ^= <<=  >>=  => The assignment operator
Left to right
and The logical operator
Left to right
Xor The logical operator
Left to right
or The logical operator
Left to right
, Used in many places

In operator precedence, or and ||, and and are logical operators that have the same effect, but have different priorities.

<?php
// 优先级: &&  >  =  >  and
// 优先级: ||  >  =  >  or
 
$a = 3;
$b = false;
$c = $a or $b;
var_dump($c);          // 这里的 $c 为 int 值3,而不是 boolean 值 true
$d = $a || $b;
var_dump($d);          //这里的 $d 就是 boolean 值 true 
?>

The output of the above examples is:

int(3)
bool(true)

The use of parentheses

We use the pairing of parentheses to clearly indicate the order of operations, rather than relying on operator precedence and binding, which usually increases the readability of the code.

<?php
// 括号优先运算
 
$a = 1;
$b = 2;
$c = 3;
$d = $a + $b * $c;
echo $d;
echo "\n";
$e = ($a + $b) * $c;  // 使用括号
echo $e;
echo "\n";
?>

The output of the above examples is:

7
9