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
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 |
##x / y | Divide the quotient of | x and y | 15 / 5 | 3 |
x % y | Modulo (remainder of division) | Remainder of x divided by y | 5 % 210 % 8 10 % 2
| 12 0
|
##- xNegation | x Negation | - 2 | -2 |
|
a . bJuxtaposition | Connect two strings | "Hi" . "Ha" | HiHa | |
The 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:
< ;?php
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 | 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 Operator | Name | Description |
##++ xPre-increment | x adds 1, then returns x | |
x ++ and then increments | returns | #-- Then decrement |
to return x, then x decreases by 1 | 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: Operator | Name | Description | Instance |
---|
x == y | Equal | If x is equal to y, return true | 5==8 Return false | x === y | Constantly equal | If x is equal to y, and they are of the same type, return true | 5==="5" Return false | x != y | Not equal to | If x is not equal to y, return true | 5!=8 Return true | x <> y | is not equal to | If x is not equal to y, return true | 5<>8 Return true | x !== y | Not equal to | If x is not equal to y, or their types are not the same, return true | 5! == "5" returns true | x > y | is greater than | If x is greater than y, returns true | 5>8 Return false | ##x < y | is less than | If x is less than y, return true | 5< 8 Return true | x >= y | is greater than or equal to | If x is greater than or equal to y, return true | 5>=8 returns false | ##x <= y is less than or equal to | If x is less than or equal to y, returns true | 5<=8 returns true | | The 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 operatorOperator | Name | Description | Instance |
---|
x and y | and | If both x and y are true , then return true | x=6 y=3 (x < 10 and y > 1) Return true | x or y | or | If at least one of x and y is true, return true | x=6 y=3 (x==6 or y==5) Return true | x xor y | XOR | If one and only one of x and y is true, then return true | x=6 y=3 (x==6 xor y==3) returns false | x && y | and | If x and y are both true, return true | x=6 y=3 (x < 10 && y > 1) Return true | ##x || y | or | If at least one of x and y is true, return true | x=6y=3 (x==5 || y==5) returns false
| ##! xnon | If x is not true, return true | x=6 | y=3!(x==y) Return true
| PHP Array Operator
OperatorName | Description | | x + yThe set | x and the set of y | | x == yare equal | If x and y have the same key/value pair, return true | | x === yIdentity | if If x and y have the same key/value pair, and the order is the same and the type is the same, then true is returned | | x != yis not equal | If x is not equal to y, return true | | ##x <> y Not equal | If x is not equal to y, return true | | x !== y Not identical | If 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; ?>
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 ?> |