Php

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 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.

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 
$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 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
$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.

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
$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.

Related Post
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
$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.

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
$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.

OperatorNameExampleResult
.Concatenation$str1 . $str2Concatenation of $str1 and $str2
.=Concatenation assignment$str1 .= $str2Appends $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.

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

$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.

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

// 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

View Comments

Recent Posts

Configure and Retrieve S3 Information Using Laravel PHP-AWS-SDK

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

3 months ago

What is the Purpose of php_eol in PHP?

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

9 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

9 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

9 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

10 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

10 months ago

Categories