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.
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 $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 ?>
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 $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 ?>
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 $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 ?>
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 $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 ?>
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 $x = 10; $y = 20 if($x < $y && ($y% $x == 0)) { echo "factored number"; } else { echo "not factored number"; } ?>
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 $x = "Hello"; $y = " PHPflow!"; echo $x . $y; // Outputs: Hello PHPflow! $x .= $y; echo $x; // Outputs: Hello PHPflow! ?>
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 |
$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 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. |
// if a is not passed $a = $a ?? 0;// result 0;
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.
echo 10 <=> 10; // outputs 0 echo 30 <=> 40; // outputs -1 echo 40 <=> 30; // outputs 1
This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More
in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More
This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More
We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More
in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More
I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More
View Comments
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