PHP operators
PHP Operators
In this chapter 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 + is used to add values together.
##PHP Arithmetic Operators
x % y Negation of x - 2
# A. B. Put two string "hi". "Ha" hiha
## :: Use%in the calculation operator, if the division is divided ($ ($ ($ ($ ($ ($ ($ ($ ($ ($ ($ ($ ($ ($ ($ ($ ($ ($ ($ ($ ($ ($ ($ ($ ($ ($ ($ ($ ($ ($ ($ ($ ($ ($) a) is a negative number, then the result obtained is also a negative value.
The following examples demonstrate different results obtained by using different arithmetic operators:
' echo ($x - $y); // 输出4 echo '
' echo ($x * $y); // 输出60 echo '
' echo ($x / $y); // 输出1.6666666666667 echo '
' echo ($x % $y); // 输出4 ?>
PHP7+ version has new integer division operators intdiv(), usage example:
The above example will output:
PHP assignment operators
## This operator is equivalent to description
# x = y x = y's left operating number is set to the right side The value of the expression
x += y #x /= y Connect two String
In PHP, the basic assignment operator is "=". It means that the left operand is set to the value of the right-hand expression. That is, the value of "$x = 5" is 5.
The following examples demonstrate different results obtained by using different assignment operators: Examples
The following Examples demonstrate different results using different string operators:
Example
x ++ Return to X, then x plus 1
- X pre-decrease x minus 1, and then return to x
# x-then decrease to X, then x minus 1 1 1 1
The following example demonstrates the results obtained using the increment/decrement operator:
Example
Operator priority
Operator priority is a very complex rule, please see
http://php .net/manual/zh/language.operators.precedence.php
No need to memorize it by rote, we only need to understand the commonly used rules, from high to low
• ! Logical NOT
• Multiplication and division operations are higher than addition and subtraction operations (multiplication and division first, then addition and subtraction)
• Comparison operators (calculate first and then compare)
• Logical operators ( Do logical operations on the comparison results)
Please analyze the result of $result = 3 * 2 + 1 > 8 || 8 / 4 - 1 == 1;
Use parentheses, even in When not strictly needed, it is usually possible to enhance the readability of the code
$result = 3 * 2 + 1 > 8 || 8 / 4 - 1 == 1;
Most A good way to write it is
$result = ((3 * 2 + 1) > 8) || ((8 / 4 - 1) == 1);
PHP Comparison Operators
Comparison operators allow you to compare two values:
Operation Operator name DescriptionIf Returns true if x is equal to y and they are of the same type 5= == "5" Return to FALSE
X! = Y is not equal to if x does not equal Y, then return True 5! = 8 Return TRUE x & LT; Y If not equal to y, return true Return to True # x & GT; Y is greater than if x is greater than y, then return True 5 & GT; 8 Return to FALSE x & LT; y to return to True 5 & LT; x>=y is less than or equal to y, then Return true 5<=8 Return true Among them, the less common ones are === and! ==. $a===$b means that $a and $b are not only numerically equal, but also have the same type. ! == and === have opposite meanings. $a!==$b means that $a and $b are not equal in value or type. In PHP, all the following values represent empty: null, false, array(), "", 0, "0".If == is used, null == false, 0 == "0", null== array(), false == ""... all return true, while using === will return false
Millions! Ten million! Don't mistakenly write == as =
This is the most common mistake for novices. = is the assignment symbol and == is the comparison for equality.
The following examples demonstrate the use of some comparison operators to obtain Different results for:
Instance
"; 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
Operator y and ## (x < 10 and y > 1) Returns true
x or y
(x==6 or y==5) Returns true
(x < 10 && y > 1) Returns true
##x || y =3 (x==5 || y==5) Returns false! x is not true If x is not true, returns true 6
y=3
!(x==y) returns true
PHP Array Operator
Operator The set of x and y y has the same key/value pairs, Returns true
x === y not equal if X is not equal to Y, then returns True
# x & LT; & GT; Y is not equal. If x is not equal to Y, then return True
x! == y , returns true
The following examples demonstrate different results obtained by using some array operators:
Example"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
Another conditional operator is "?:" (or triple meta) operator.Grammar format
(expr1) ? (expr2) : (expr3)
The value when expr1 evaluates to TRUE is expr2, and when expr1 evaluates to FALSE the value is expr3.
Since PHP 5.3, the middle part of the ternary operator can be omitted. The expression expr1 ?: expr3 returns expr1 if expr1 evaluates to TRUE and expr3 otherwise.
Example
In the following example, it is judged that the $_GET request contains the user value. If so, $_GET['user'] is returned, otherwise nobody is returned:
Welcome to learn PHP
Welcome to learn PHP
Note: PHP_EOL is a newline character and is compatible with larger platforms.
There is an additional NULL merge operator in the PHP7+ version. The example is as follows:
Combined comparison operator (PHP7+)
PHP7+ supports Combined comparison operators, examples are as follows:
1; // 0 echo 1 <=> 2; // -1 echo 2 <=> 1; // 1 // 浮点型 echo 1.5 <=> 1.5; // 0 echo 1.5 <=> 2.5; // -1 echo 2.5 <=> 1.5; // 1 // 字符串 echo "a" <=> "a"; // 0 echo "a" <=> "b"; // -1 echo "b" <=> "a"; // 1 ?>