In this chapter we will discuss the use of different operators in PHP.
In PHP, the assignment operator = is used to assign a value to a variable.
In PHP, the arithmetic operator + is used to add values together.
operator | name | describe | Example | result |
---|---|---|---|---|
x + y | add | sum of x and y | 2+2 | 4 |
x-y | reduce | difference between x and y | 5-2 | 3 |
x*y | take | product of x and y | 5*2 | 10 |
x/y | remove | Quotient of x and y | 15/5 | 3 |
x % y | Modulo (remainder of division) | Remainder when x is divided by y | 5% 210% 810% 2 | 120 |
-x | negate | negate x | -2 | -2 |
a.b | juxtapose | Concatenate two strings | "Hi" . "Ha" | HiHa |
The following examples demonstrate different results using different arithmetic operators:
<?php $x = 10 ; $y = 6 ; echo ( $x + $y ) ; // Output 16 echo ' <br> ' ; // Line break echo ( $x - $y ) ; // Output 4 echo ' <br> ' ; // Line break echo ( $x * $y ) ; // Output 60 echo ' <br> ' ; // Line break echo ( $x / $y ) ; // Output 1.6666666666667 echo ' <br> ' ; // Line break echo ( $x % $y ) ; // Output 4 echo ' <br> ' ; // Line break echo - $x ; ?>
The PHP7+ version has a new integer division operator intdiv() , usage examples:
<?php var_dump ( intdiv ( 10 , 3 ) ) ; ?>
The above example will output:
int(3)
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 | Equivalent to | describe |
---|---|---|
x = y | x = y | The left operand is set to the value of the right-hand expression |
x += y | x = x + y | add |
x -= y | x = x - y | reduce |
x*=y | x = x * y | take |
x /= y | x = x / y | remove |
x%=y | x = x % y | Modulo (remainder of division) |
a .= b | a = a . b | Concatenate two strings |
The following examples demonstrate different results using different assignment operators:
<?php $x = 10 ; echo $x ; // Output 10 $y = 20 ; $y += 100 ; echo $y ; // Output 120 $z = 50 ; $z -= 25 ; echo $z ; // Output 25 $i = 5 ; $i *= 6 ; echo $i ; // Output 30 $j = 10 ; $j /= 5 ; echo $j ; // Output 2 $k = 15 ; $k %= 4 ; echo $k ; // Output 3 ?>
The following examples demonstrate the same result using different string operators:
<?php $a = " Hello " ; $b = $a . " world! " ; echo $b ; // Output Hello world! $x = " Hello " ; $x .= " world! " ; echo $x ; // Output Hello world! ?>
operator | name | describe |
---|---|---|
++x | pre-increment | Add 1 to x and return x |
x++ | post-increment | Return x, then add 1 to x |
--x | Pre-decrement | Decrement x by 1, then return x |
x -- | Decreasing after | Returns x, then decrements x by 1 |
The following example demonstrates the results of using the increment/decrement operators:
<?php $x = 10 ; echo ++ $x ; // Output 11 $y = 10 ; echo $y ++; // Output 10 $z = 5 ; echo -- $z ; // Output 4 $i = 5 ; echo $i --; // Output 5 ?>
Comparison operators let you compare two values:
operator | name | describe | Example |
---|---|---|---|
x == y | equal | Returns true if x equals y | 5==8 returns false |
x === y | absolutely equal to | Returns true if x is equal to y and they are of the same type | 5==="5" returns false |
x != y | not equal to | Returns true if x is not equal to y | 5!=8 returns true |
x <> y | not equal to | Returns true if x is not equal to y | 5<>8 returns true |
x !== y | Definitely not equal to | Returns true if x is not equal to y, or they are not of the same type | 5!=="5" returns true |
x > y | greater than | Returns true if x is greater than y | 5>8 returns false |
x < y | less than | Returns true if x is less than y | 5<8 returns true |
x >= y | Greater than or equal to | Returns true if x is greater than or equal to y | 5>=8 returns false |
x <= y | less than or equal to | Returns true if x is less than or equal to y | 5<=8 returns true |
The following examples demonstrate different results using some comparison operators:
<?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 ) ; ?>
operator | name | describe | Example |
---|---|---|---|
x and y | and | Returns true if both x and y are true | x=6y=3(x < 10 and y > 1) returns true |
x or y | or | Returns true if at least one of x and y is true | x=6y=3(x==6 or y==5) returns true |
x xor y | XOR | If one and only one of x and y is true, then return true | x=6y=3(x==6 xor y==3) returns false |
x && y | and | Returns true if both x and y are true | x=6y=3(x < 10 && y > 1) returns true |
x || y | or | Returns true if at least one of x and y is true | x=6y=3(x==5 || y==5) returns false |
!x | No | If x is not true, return true | x=6y=3!(x==y) returns true |
operator | name | describe |
---|---|---|
x + y | gather | the set of x and y |
x == y | equal | Returns true if x and y have the same key/value pair |
x === y | Equality | Returns true if x and y have the same key/value pairs in the same order and type |
x != y | Not equal | Returns true if x is not equal to y |
x <> y | Not equal | Returns true if x is not equal to y |
x !== y | Not identical | Returns true if x is not equal to y |
The following examples demonstrate different results using some array operators:
<?php $x = array ( " a " => " red " , " b " => " green " ) ; $y = array ( " c " => " blue " , " d " => " yellow " ) ; $z = $x + $y ; // Combine $x and $y arrays var_dump ( $z ) ; var_dump ( $x == $ y ) ; var_dump ( $x === $y ) ; var_dump ( $ x ! = $ y ) ; var_dump ( $ x < > $y ) ; var_dump ( $ x !== $y ) ; ?>
Another conditional operator is the "?:" (or ternary) operator.
(expr1) ? (expr2) : (expr3)
The value when expr1 evaluates to TRUE is expr2, and when expr1 evaluates to FALSE the value is expr3.
As of 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.
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 = ' Coder Tutorial ' ; // Normal writing method $username = isset ( $test ) ? $test : ' nobody ' ; echo $username , PHP_EOL ; // PHP 5.3+ version writing method $username = $test ?: ' nobody ' ; echo $username , PHP_EOL ; ?>
Tutorials for Coders Coders Tutorials
Note: PHP_EOL is a newline character, compatible with larger platforms.
There is an additional NULL coalescing operator ??
in the PHP7+ version. 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 ' ; ?>
PHP7+ supports combined comparison operators, examples are as follows:
<?php // Integer type 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 ?>
The following table lists the operators from highest to lowest precedence. Operators in the same line have the same precedence, and the direction in which they are combined determines the order of evaluation.
Note : Left = from left to right, right = from right to left.
Binding direction | operator | Additional Information |
---|---|---|
none | clone new | clone and new |
Left | [ | array() |
right | ++ -- ~ (int) (float) (string) (array) (object) (bool) @ | Types and increment/decrement |
none | instanceof | type |
right | ! | Logical operators |
Left | */% | arithmetic operators |
Left | + – . | Arithmetic and string operators |
Left | << >> | Bit operators |
none | == != === !== <> | comparison operator |
Left | & | Bit operators and references |
Left | ^ | Bit operators |
Left | | | Bit operators |
Left | && | Logical operators |
Left | || | Logical operators |
Left | ? : | ternary operator |
right | = += -= *= /= .= %= &= |= ^= <<= >>= => | assignment operator |
Left | and | Logical operators |
Left | xor | Logical operators |
Left | or | Logical operators |
Left | , | Used in many places |
In operator precedence, or, ||, && and and are all logical operators with the same effect, but their priorities are different.
<?php // Priority: && > = > and // Priority: || > = > or $a = 3 ; $b = false ; $c = $a or $b ; var_dump ( $c ) ; // $c here is the int value 3, not the boolean value true $d = $a || $b ; var_dump ( $d ) ; // $d here is the boolean value true ?>
The output result of the above example is:
int(3)bool(true)
We clearly indicate the order of operations through the pairing of brackets, rather than relying on operator precedence and associativity, which can usually increase the readability of the code.
<?php // Bracket priority operation $a = 1 ; $b = 2 ; $c = 3 ; $d = $a + $b * $c ; echo $d ; echo " n " ; $e = ( $a + $b ) * $c ; // Use parentheses echo $e ; echo " n " ; ?>
The output result of the above example is:
79