May 10, 2021 PHP
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.
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:
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:
The following example demonstrates the same results with different string operators:
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:
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:
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 |
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:
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.
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:
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)
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