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:
<?php $x=10; $y=6; echo ($x + $y); // 輸出16 echo '<br/>' echo ($x - $y); // 輸出4 echo '<br/>' echo ($x * $y); // 輸出60 echo '<br/>' echo ($x / $y); // 輸出1.6666666666667 echo '<br/>' echo ($x % $y); // 輸出4 ?>
PHP7+ version has new integer division operators intdiv(), usage example:
<?php var_dump(intdiv(10, 3)); ?>
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<?php $x=10; echo $x; // 輸出10 $y=20; $y += 100; echo $y; // 輸出120 $z=50; $z -= 25; echo $z; // 輸出25 $i=5; $i *= 6; echo $i; // 輸出30 $j=10; $j /= 5; echo $j; // 輸出2 $k=15; $k %= 4; echo $k; // 輸出3 ?>
The following Examples demonstrate different results using different string operators:
Example
<?php $a = "Hello"; $b = $a . " world!"; echo $b; // 輸出Hello world! $x="Hello"; $x .= " world!"; echo $x; // 輸出Hello world! ?>
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
<?php $x=10; echo ++$x; // 輸出11 $y=10; echo $y++; // 輸出10 $z=5; echo --$z; // 輸出4 $i=5; echo $i--; // 輸出5 ?>
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
<?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); ?>
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<?php $x = array("a" => "red", "b" => "green"); $y = array("c" => "blue", "d" => "yellow"); $z = $x + $y; // $x 和 $y 數(shù)組合并 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:
<?php $test = '歡迎學(xué)習(xí)PHP'; // 普通寫法 $username = isset($test) ? $test : 'nobody'; echo $username, PHP_EOL; // PHP 5.3+ 版本寫法 $username = $test ?: 'nobody'; echo $username, PHP_EOL; ?>
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:
<?php // 如果 $_GET['user'] 不存在返回 'nobody',否則返回 $_GET['user'] 的值 $username = $_GET['user'] ?? 'nobody'; // 類似的三元運(yùn)算符 $username = isset($_GET['user']) ? $_GET['user'] : 'nobody'; ?>
Combined comparison operator (PHP7+)
PHP7+ supports Combined comparison operators, examples are as follows:
<?php // 整型 echo 1 <=> 1; // 0 echo 1 <=> 2; // -1 echo 2 <=> 1; // 1 // 浮點(diǎn)型 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 ?>