PHP operators include:
- Arithmetic operators
- Assignment operators
- Comparison operators
- The ternary operator
- Logical operators
- String operators
- Error Control Operators
|+||Add||$x = 2; |
$y = $x + 1;
|$y = 3|
|-||Subtract||$x = 2; |
$y = $x-1;
|$y = 1|
|*||multiplied||$x = 2; |
$y = $x * 3;
|$y = 6|
|/||except that the result is a floating point type||$x = 6; |
$y = $x /3;
|$y = 2|
|%||for more than, the return result is a floating point type||$x = 6; |
$y = $x% 4;
|$y = 2|
|++||Add, divided into before and after adding |
on the Boolean and NULL is invalid
| $x++;(after the addition) $x = 2; |
|$x = 3|
|-||Decrement, divided into pre-decrement and post-decrement |
is invalid for Boolean and NULL
|$x = 2; |
- $x;(before subtraction)
|$x = 1|
For the former plus after the increase, after the implementation of the results are variables plus 1, the difference is that the implementation of the results are not the same return, refer to the following two examples:
$x = 2; echo $x++; // output 2 echo $x; // Output 3 $x = 2; echo ++$x; // Output 3 echo $x; // Output 3
Decrease the same token.
Assignment operator "=" for assignment operations, the assignment of the role of the operator is to the right of the value assigned to the left variables:
$x = 2;
This example assigns the integer value 2 to the variable $x.
Assignment operator other usage
Assignment operators can be nested for use:
$y = ($x = 2) + 5; // The result is $x = 2, $y = 7
Some Variants of the Assignment Operator Usage:
$y += $x;
In fact it is equivalent to:
$y = $y + $x;
Similarly there are:
$y -= $x; $y *= $x; $y /= $x; $y %= $x; $y .= $x; // For the concatenation operator, see the string operator
|==||equals||2 == 3||FALSE|
|!=||is not equal or can be written as < >||2 == 3||TRUE|
|>||greater than||2 > 3||FALSE|
|<||less than||2 < 3||TRUE|
|>=||greater than or equal to||2 > = 3||FALSE|
|<=||less than or equal||2 < = 3||TRUE|
The comparison operators can also be used for string comparisons.
Ternary can be regarded as a special comparison operator:
Syntax Explanation: The value of expr2 is expr2 when expr1 evaluates to TRUE, or expr3 otherwise.
$y = ($x == 2) $x: 1;
This example determines whether the value of the variable $x is equal to 2, and if $x is equal to 2, then the value of $y is equal to $x(that is, equal to 2) and vice versa.
In order to avoid errors, it is a good idea to enclose the expressions of the ternary operator in parentheses.
|&&||Logical AND, can also be written and||$x = 2; |
$y = 6;
$x &&$y > 5
|||||Logical OR, can also be written as or||$x = 2; |
$y = 6;
$x &&$y > 5
|!||logical not, take the logical opposite||$x = 2; |
$y = 6;
!($x > $y)
String operator mainly refers to the connection operator ".", It will connect its left and right parameter string.
$x = " London "; $y = $x. &Quot; Hello " // $y = " London hello "
As shown in this example, the concatenation operator is required when outputting characters and variables together.
The concatenation operator, along with the assignment operator, forms the join assignment operator:
$x = " London "; $x. = &Quot; Hello " // $x = " London hello "
Error Control Operators
The error control operator: @, when placed in a PHP expression before the expression may have any error messages are ignored.
/* To read the file * / $my_file = @file('file1') or die(" open file failed, error message: '$php_errormsg' ");
The error control operator is often used to block PHP system errors in the places where errors may occur and to prompt a friendly error message.