This tutorial will describe PHP 7 operators with example. The Operators help to perform operations on variables and values.
There are some new operators introduced into php 7, like the null coalescing operator (??
), spaceship operator(<=>
).
Here, We will discuss all php7 operators with examples. We will go through one by one operator types in PHP 7. There are the following operator groups available in PHP 7.
- Arithmetic operators
- Assignment operators
- Comparison operators
- Increment/Decrement operators
- Logical operators
- String operators
- Array operators
- Conditional assignment operators
- Spaceship operators
PHP Arithmetic Operators
These methods help to do some common arithmetical operations onto the variables. You can do addition, subtraction, multiplication, etc. between two variables.
Operator | Name | Example | Description |
---|---|---|---|
+ | Addition | $x + $y | Sum of $x and $y |
– | Subtraction | $x – $y | Difference of $x and $y |
* | Multiplication | $x * $y | Product of $x and $y |
/ | Division | $x / $y | Quotient of $x and $y |
% | Modulus | $x % $y | Remainder of $x divided by $y |
** | Exponentiation | $x ** $y | Result of raising $x to the $y’th power |
PHP 7 Arithmetic Operators Example
<?php $x = 20; $y = 8; echo($x + $y); // 0utput: 28 echo($x - $y); // 0utput: 12 echo($x * $y); // 0utput: 160 echo($x / $y); // 0utput: 2.5 echo($x % $y); // 0utput: 4 ?>
PHP 7 Assignment Operators
This type php 7 operator are used to assign values to variables.The left operands gets set to the value of the assignment expression on the right.
Assignment Operator | Same as | Description |
---|---|---|
$x = $y | $x = $y | The assign right expression values to the left variable. |
$x += $y | $x = $x + $y | Addition and assign value to left variable($x). |
$x -= $y | $x = $x – $y | Subtraction and assign value to left variable($x) |
$x *= $y | $x = $x * $y | Multiplication and assign value to $x. |
$x /= $y | $x = $x / $y | Division and assign value to $x |
$x %= $y | $x = $x % $y | Modulus and assign value to $x |
PHP 7 Assignment Operators Example
<?php $x = 20; echo $x; // Outputs: 20 $x += 30; echo $x; // Outputs: 50 $x -= 20; echo $x; // Outputs: 30 $x *= 5; echo $x; // Outputs: 150 $x /= 10; echo $x; // Outputs: 15 $x %= 15; echo $x; // Outputs: 1 ?>
PHP Comparison Operators
The PHP 7 Comparison Operators help to compare two variables. You can also compare two different types of variables as well.
Operator | Name | Uses | Result |
---|---|---|---|
== | Equal | $x == $y | Returns true, if $x is equal to $y |
=== | Identical | $x === $y | Returns true, if $x is equal to $y with same type |
!= | Not equal | $x != $y | Returns true, if $x is not equal to $y |
<> | Not equal | $x <> $y | Returns true, if $x is not equal to $y |
!== | Not identical | $x !== $y | Returns true, if $x is not equal to $y, or they are not of the same type |
> | Greater than | $x > $y | Returns true, if $x is greater than $y |
< | Less than | $x < $y | Returns true if, $x is less than $y |
>= | Greater than or equal to | $x >= $y | Returns true, if $x is greater than or equal to $y |
<= | Less than or equal to | $x <= $y | Returns true, if $x is less than or equal to $y |
PHP 7 comparison Operators Example
<?php $x = 5; $y = 15; $z = "5"; var_dump($x == $z); // Outputs: boolean true var_dump($x === $z); // Outputs: boolean false var_dump($x < $y); // Outputs: boolean true var_dump($x <= $y); // Outputs: boolean true ?>
PHP 7 Increment / Decrement Operators
The PHP 7 increment operators are used to increment a variable’s value whereas Decrement Operators used to decrement a variable’s value.
Operator | Name | Description |
---|---|---|
++$x | Pre-increment | Increments $x by one, then returns $x |
$x++ | Post-increment | Returns $x, then increments $x by one |
–$x | Pre-decrement | Decrements $x by one, then returns $x |
$x– | Post-decrement | Returns $x, then decrements $x by one |
PHP 7 Incrementing and Decrementing Operators Example
<?php $x = 10; echo ++$x; // Outputs: 11 echo $x; // Outputs: 11 echo $x++; // Outputs: 11 echo $x; // Outputs: 12 echo --$x; // Outputs: 11 echo $x; // Outputs: 11 echo $x--; // Outputs: 11 echo $x; // Outputs: 9 ?>
PHP 7 Logical Operators
The PHP 7 logical operators are used to combine two conditions. You can combine two variable conditions using logical operators.
Operator | Name | Example | Result |
---|---|---|---|
and | And | $x and $y | True – if both $x and $y are true |
or | Or | $x or $y | True – if either $x or $y is true |
xor | Xor | $x xor $y | True – if either $x or $y is true, but not both |
&& | And | $x && $y | True – if both $x and $y are true |
|| | Or | $x || $y | True – if either $x or $y is true |
! | Not | !$x | True – if $x is not true |
PHP 7 Logical Operators Example
<?php $x = 10; $y = 20 if($x < $y && ($y% $x == 0)) { echo "factored number"; } else { echo "not factored number"; } ?>
PHP 7 String Operators
There are the following operators for string manipulation.Its specially designed for string types variables.
Operator | Name | Example | Result |
---|---|---|---|
. | Concatenation | $str1 . $str2 | Concatenation of $str1 and $str2 |
.= | Concatenation assignment | $str1 .= $str2 | Appends $str2 to $str1 |
PHP 7 String Operators Example
<?php $x = "Hello"; $y = " PHPflow!"; echo $x . $y; // Outputs: Hello PHPflow! $x .= $y; echo $x; // Outputs: Hello PHPflow! ?>
PHP 7 Array Operators
PHP 7 also provides operators for array type variables, you can compare two arrays using the following array operators.
Operator | Name | Example | Result |
---|---|---|---|
+ | Union | $x + $y | Union of $x and $y |
== | Equality | $x == $y | Returns true, if $x and $y have the same key/value pairs |
=== | Identity | $x === $y | Returns true, if $x and $y have the same key/value pairs in the same order and of the same types |
!= | Inequality | $x != $y | Returns true, if $x is not equal to $y |
<> | Inequality | $x <> $y | Returns true, if $x is not equal to $y |
!== | Non-identity | $x !== $y | Returns true, if $x is not identical to $y |
PHP 7 String Operators Example
$a = array("a" => "Car", "b" => "Bus", "c" => "Truck"); $b = array("u" => "Auto", "v" => "Train", "w" => "Metro"); $c = $a + $b; // Union of $x and $y var_dump($c); var_dump($a == $b); // Outputs: boolean false var_dump($a === $b); // Outputs: boolean false var_dump($a != $b); // Outputs: boolean true var_dump($a <> $b); // Outputs: boolean true var_dump($a !== $b); // Outputs: boolean true
PHP 7 Conditional Assignment Operators
PHP provides some conditional statements for value assignment of a variable. These conditional assignment operators are used to set a value depending on conditions.
Operator | Name | Example | Result |
---|---|---|---|
?: | Ternary | $x = 3 > 2 ? true : false | Returns the value of $x based on condition. |
?? | Null coalescing | $x = $a > $b ?? $a : $b | Returns the value of $x. The value of $x is $a if $a exists, and is not NULL. If $a does not exist, or is NULL, the value of $x is $b. |
PHP 7 Conditional Assignment Operators Example
// if a is not passed $a = $a ?? 0;// result 0;
PHP 7 Spaceship Operator
These operators are introduced into PHP 7. The operand (<=>) is used for comparing two expressions. This is a three-way comparison operator and it can perform greater than, less than, and equal comparisons between two operands.
The spaceship operator returns 0 if both operands are equal, 1 if the left is greater, and -1 if the right is greater.
PHP 7 Spaceship Operator Example
echo 10 <=> 10; // outputs 0 echo 30 <=> 40; // outputs -1 echo 40 <=> 30; // outputs 1
there are errors:
echo($x % $y); // 0utputs: 4 not 2
——————
$a = $a ?? $a : 0;// result 0;
it must be written
$a = $a ??0;// result 0;
thanks for correction