PHP 7 Operators with Example

This tutorial will describe PHP 7 operators with example.The Operators help to perform operations on variables and values.

There are some new operator introduced into php 7, like null coalescing operator (??), spaceship operator(<=>).

Here, We will discuss all php7 operators with example.We will go through one by one operator types in PHP 7.There are following operators 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 method help to do some common arithmetical operations onto the variables.You can do addition, subtraction, multiplication etc. between two variables.

OperatorNameExampleDescription
+Addition$x + $ySum of $x and $y
Subtraction$x – $yDifference of $x and $y
*Multiplication$x * $yProduct of $x and $y
/Division$x / $yQuotient of $x and $y
%Modulus$x % $yRemainder of $x divided by $y
**Exponentiation$x ** $yResult of raising $x to the $y’th power

PHP 7 Arithmetic Operators Example

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 OperatorSame asDescription
$x = $y$x = $yThe assign right expression values to the left variable.
$x += $y$x = $x + $yAddition and assign value to left variable($x).
$x -= $y$x = $x – $ySubtraction and assign value to left variable($x)
$x *= $y$x = $x * $yMultiplication and assign value to $x.
$x /= $y$x = $x / $yDivision and assign value to $x
$x %= $y$x = $x % $yModulus and assign value to $x

PHP 7 Assignment Operators Example

PHP Comparison Operators

The PHP 7 Comparison Operators help to compare two variables.You can also compare two different type variables as well.

OperatorNameUsesResult
==Equal$x == $yReturns true, if $x is equal to $y
===Identical$x === $yReturns true, if $x is equal to $y with same type
!=Not equal$x != $yReturns true, if $x is not equal to $y
<>Not equal$x <> $yReturns true, if $x is not equal to $y
!==Not identical$x !== $yReturns true, if $x is not equal to $y, or they are not of the same type
>Greater than$x > $yReturns true, if $x is greater than $y
<Less than$x < $yReturns true if, $x is less than $y
>=Greater than or equal to$x >= $yReturns true, if $x is greater than or equal to $y
<=Less than or equal to$x <= $yReturns true, if $x is less than or equal to $y

PHP 7 comparison Operators Example

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.

OperatorNameDescription
++$xPre-incrementIncrements $x by one, then returns $x
$x++Post-incrementReturns $x, then increments $x by one
–$xPre-decrementDecrements $x by one, then returns $x
$x–Post-decrementReturns $x, then decrements $x by one

PHP 7 Incrementing and Decrementing Operators Example

PHP 7 Logical Operators

The PHP 7 logical operators are used to combine two conditions.You can combine two variable conditions using logical operators.

OperatorNameExampleResult
andAnd$x and $yTrue – if both $x and $y are true
orOr$x or $yTrue – if either $x or $y is true
xorXor$x xor $yTrue – if either $x or $y is true, but not both
&&And$x && $yTrue – if both $x and $y are true
||Or$x || $yTrue – if either $x or $y is true
!Not!$xTrue – if $x is not true

PHP 7 Logical Operators Example

PHP 7 String Operators

There are following operators for string manipulation.Its specially designed for string types variables.

OperatorNameExampleResult
.Concatenation$str1 . $str2Concatenation of $str1 and $str2
.=Concatenation assignment$str1 .= $str2Appends $str2 to $str1

PHP 7 String Operators Example

PHP 7 Array Operators

PHP 7 also provides operators for array type variables, you can compare two arrays using following array operators.

OperatorNameExampleResult
+Union$x + $yUnion of $x and $y
==Equality$x == $yReturns true, if $x and $y have the same key/value pairs
===Identity$x === $yReturns true, if $x and $y have the same key/value pairs in the same order and of the same types
!=Inequality$x != $yReturns true, if $x is not equal to $y
<>Inequality$x <> $yReturns true, if $x is not equal to $y
!==Non-identity$x !== $yReturns true, if $x is not identical to $y

PHP 7 String Operators Example

PHP 7 Conditional Assignment Operators

PHP provides some conditional statements for values assignment of a variable. This conditional assignment operators are used to set a value depending on conditions.

OperatorNameExampleResult
?:Ternary$x = 3 > 2 ? true : falseReturns the value of $x based on condition.
??Null coalescing$x = $a > $b ?? $a : $bReturns 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

PHP 7 Spaceship Operator

These operator are introduced into PHP 7.The operand (<=>) used for comparing two expressions.This is a three-way comparison operator and it can perform greater than, less than and equal comparison 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

2 thoughts on “PHP 7 Operators with Example

  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;

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.