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.
What are the PHP operators?
php operators include arithmetic operators, assignment operators, increment\decrement operators, comparison operators, logical operators, array operators, ternary operators, and combined comparison operators.
PHP Arithmetic Operators
##x / yDivide the quotient of x and y15 / 5 3x % yModulo (remainder of division)Remainder of x divided by y5 % 21##- xa . bThe following examples demonstrate different results obtained by using different arithmetic operators:
Example
<?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 ?>
Running Example»
Click the "Run Example" button to view the online example
PHP7+ version has a new integer division operatorintdiv(), usage example:
var_dump(intdiv(10, 3));
?>
The above example will output:
PHP assignment operator
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.
Operator | Name | Description | Example | Result |
---|---|---|---|---|
x + y | Add the sum of | x and y | 2 + 2 | 4 |
x - y | minus the difference between | x and y | 5 - 2 | 3 |
x * y | multiplied by the product of | x and y | 5 * 2 | 10 |
10 % 8 10 % 2 | 2 0 | |||
Negation | x Negation | - 2 | -2 | |
Juxtaposition | Connect two strings | "Hi" . "Ha" | HiHa |
Operator | is equivalent to | Description |
---|---|---|
x = y | x = y | The left operand is set to the value of the right-hand expression |
x += y | x = x + y | plus |
x -= y | x = x - y | minus |
x *= y | #x = x * y | times |
x /= y | x = x / y | Division |
x %= y | x = x % y | modulo (remainder of division) |
a .= b | a = a . b | Concatenate two strings |
The following examples demonstrate different results obtained by using different assignment operators:
Example
<?php $x=10; echo $x; // 输出10 echo "<br>"; $y=20; $y += 100; echo $y; // 输出120 echo "<br>"; $z=50; $z -= 25; echo $z; // 输出25 echo "<br>"; $i=5; $i *= 6; echo $i; // 输出30 echo "<br>"; $j=10; $j /= 5; echo $j; // 输出2 echo "<br>"; $k=15; $k %= 4; echo $k; // 输出3 ?>
Run Example»
Click "Run Example" button to view online examples
The following examples demonstrate different results obtained by using different string operators:
Example
<?php $a = "Hello"; $b = $a . " world!"; echo $b; // 输出Hello world! echo "<br>"; $x="Hello"; $x .= " world!"; echo $x; // 输出Hello world! ?>
Run instance»
Click the "Run instance" button to view the online instance
##PHP increment/decrement operator OperatorNameDescription##++ xx ++ to return x, then x decreases by 1
Pre-increment | x adds 1, then returns x | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
and then increments | returns | #-- Then decrement | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
The following example demonstrates the results obtained using the increment/decrement operator: Example<?php $x=10; echo ++$x; // 输出11 echo "<br>"; $y=10; echo $y++; // 输出10 echo "<br>"; $z=5; echo --$z; // 输出4 echo "<br>"; $i=5; echo $i--; // 输出5 ?> Running Example» Click the "Run Example" button to view the online example PHP Comparison OperatorThe comparison operator allows you to compare two values: ##x < y is less than If x is less than y, return true5< 8 Return truex >= y is greater than or equal to If x is greater than or equal to y, return true 5>=8 returns false##x <= yThe following examples demonstrate the different results obtained using some comparison operators: Example<?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); ?> Running Example» Click the "Run Instance" button to view the online instance PHP logical operator
Operatorx + yx == yx === yx != y##x <> yNot equalIf x is not equal to y, return true x !== yNot identicalIf x is not equal to y, return true The following examples demonstrate different results obtained using some array operators: Example<?php $x = array("a" => "red", "b" => "green"); $y = array("c" => "blue", "d" => "yellow"); $z = $x + $y; // $x 和 $y 数组合并 var_dump($z); echo "<br>"; var_dump($x == $y); echo "<br>"; var_dump($x === $y); echo "<br>"; var_dump($x != $y); echo "<br>"; var_dump($x <> $y); echo "<br>"; var_dump($x !== $y); ?> Running Example» Click the "Run Example" button to view the online example Ternary OperatorAnother conditional operator is the "?:" (or ternary) operator . Syntax format(expr1) ? (expr2) : (expr3) When expr1 evaluates to TRUE, the value is expr2. The value of expr1 when evaluating to FALSE 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. ExampleIn 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 = 'php中文网'; // Common writing method $username = isset($test) ? $test : 'nobody'; echo $username, PHP_EOL ; // PHP 5.3+ version writing method $username = $test ?: 'nobody'; echo $username, PHP_EOL; ?> php中文网 php中文网 Note: PHP_EOL is a newline character and is compatible with larger platforms. In the PHP7+ version, there is an additional NULL merge operator. The example is as follows: <?php // If $_GET['user'] does not exist, return ' nobody', otherwise return the value of $_GET['user'] $username = $_GET['user'] ?? 'nobody'; // Similar ternary operator $username = isset($_GET['user']) ? $_GET['user'] : 'nobody'; ?> Combined comparison operator (PHP7+)PHP7+ supports combined comparison operators, examples are as follows: <?php // Integer echo 1 <=> 1; // 0 echo 1 <=> 2; // -1 echo 2 <=> 1; // 1 // Floating point type echo 1.5 <=> 1.5 ; // 0 echo 1.5 <=> 2.5; // -1 echo 2.5 <=> 1.5; // 1 // String echo " a" <=> "a"; // 0 echo "a" <=> "b"; // -1 echo "b" <=> "a"; // 1 ?>
|