PHP Operators are key words that are used to perform operations on operands (variables). PHP operators have been divided into 7 groups. Those are listed here.

PHP Operators :

  • PHP Arithmetic Operators
  • PHP Assignment operators
  • PHP Comparison operators
  • PHP Increment/Decrement operators
  • PHP Logical operators
  • PHP String operators
  • PHP Array operators

PHP Arithmetic Operators :

PHP Arithmetic Operators are used to perform the mathematical manipulations on variables.

They include Addition, Subtraction, Multiplication, Division, Modulus and Exponential. Lets discuss each and every operations with example.


<?php

$x = 78; $y = 12;

echo "Addition : ",$x+$y,"</br>";

echo "Subtraction : ",$x-$y,"</br>";

echo "Multiplication : ",$x*$y,"</br>";

echo "Division : ",$x/$y,"</br>";

echo "Modulus : ",$x%$y,"</br>";

echo "Exponential : ",$x^$y,"</br>";

?>

output:

Addition : 90
Subtraction : 66
Multiplication : 936
Division : 6.5
Modulus : 6
Exponential : 66

PHP Assignment operators

PHP Assignment operators are used to to assign values to variables. The variable on the left side of the operand is updated with the value on the right side.

Includes the Equal sign as the basic assignment operator.


<?php
 $x = 78; $y = 12;
 echo "$x value is : ",$x; ?>

On the above example, we have assigned the 78 value to $x variable.

We can combine the two different php operators like Assignment operators with Arithmetic operators. Here is the example :

 <?php
 $x = 78; 
echo " x +1 value is : ",$x+=1,""; 
echo " x -1 value is : ",$x-=1,"";
echo " x / 1 value is : ",$x/=1,"";
echo " x * 1 value is : ",$x*=1,""; 
echo " x % 1 value is : ",$x%=1,""; 
?>
 

output:

x +1 value is : 79
x -1 value is : 78
x / 1 value is : 78
x * 1 value is : 78
x % 1 value is : 0

PHP Comparison operators

Comparison operator shows the relationship shared by two or more values, either a number or a string. Simply these php operators are used to compare the two values either numbers or strings. Here we are going to tell you, how comparisons are done using php operators.

Equal (==) :

It compares the content of two variables ($a,$b) are equal or not. Returns true if $a is equal to $b.

Identical (===) :

It compares the content of two variables ($a,$b) are equal or not like equals (==) operator. And it is also compares the type of the variable also. Returns true if $a is equal to $b, and they are of the same type


<?php
 $a = 50;   $b = "50"; 
var_dump($x === $y); 
// returns false because types are not equal ?> 

Not equal (!=) :

It compares the content of two variables ($a,$b). Returns true if $a is not equal to $b.

Not equal (<>) :

It compares the content of two variables ($a,$b). Returns true if $a is not equal to $b.

Not identical (!===) :

It compares the content of two variables ($a,$b) with type , like equivalent (===) operator. Returns true if $a is not equal to $a, or they are not of the same type.

Greater than (>) :

It compares whether the left hand side variable is greater than left hand side variable ($a > $b).  Returns true if $a is greater than $b.

Less than (<) :

It compares whether the left hand side variable is less than left hand side variable ($a < $b).  Returns true if $a is less than $b.

Greater than or equal to (>=) :

It compares whether the left hand side variable is greater than or equals to left hand side variable ($a >= $b).  Returns true if $a is greater than or equals $b.

Less than or equal to (<=) :

It compares whether the left hand side variable is less than or equals to left hand side variable ($a >= $b).  Returns true if $a is less than or equals $b.

PHP Increment/Decrement operator :

Increment/Decrement operators are operators used to increase or decrease a value continuously by a factor of 1. Pre-increment operator Increases the value by one then returns the new value of $x

Example :


<?php
$x = 78;
echo ++$x;
echo "<br>";
echo $x;
?>

Output :

79
79

Post-increment returns $x then increments the value by 1.

Example :


<?php
$x = 78;
echo $x++;
echo "<br>";
echo $x;
?>

Output :

78
79

The same situation and syntax happens for the decrement operator. The only change is that the sign changes from $x++ and ++$x to $x—and –$x. Here you can see how increment and decrement operators are works in Java.

Logical operators :

Logical operators are used to imply the relationship of two values by conditional statement.

and operator :

And operator is used to show a compulsory “true” value for both values. Also $x && $y.

True if both $x and $y are true

Example:


<?php
$x = 78;
$y = 12;

if ($x == 78 and $y == 12) {
echo "The value is parses correctly";
}
?>

output:

The value is parses correctly

OR operator :

Syntax : $x or $y;

Is used to show that one of the two values must be true. Also $x || $y

True if either $x or $y is true

Xor operator :

Syntax : $x xor $y

True if either $x or $y is true, but not both

Not operator :

Syntax : !$x

True if $x is not true

String operators :

There are two operators for strings in PHP.  Concatenation is the merging of two strings by using the full stop between them.

 <?php $txt1 = "Hello, ";</p>
<p class="EnlighterJSRAW" data-enlighter-language="generic">$txt2 = "Its nice to meet you!";</p>
<p class="EnlighterJSRAW" data-enlighter-language="generic">echo $txt1 . $txt2;</p>
<p class="EnlighterJSRAW" data-enlighter-language="generic">?></p>
<p class="EnlighterJSRAW" data-enlighter-language="generic">

output:

Hello, Its nice to meet you!

Array operators :

PHP Array operators are used to compare the two arrays. Below are the major array operators.

Union operator :

In php we can make the union operation using the “+” sign. It unions (combines) the two arrays.


<?php
$x = array("r" => "red", "b" => "blue");
$y = array("g" => "green", "y" => "yellow");

var_dump($x + $y);  //  unions the $x and $y
?>

Output :

array(4) { ["r"]=> string(3) "red" ["b"]=> string(4) "blue" ["g"]=> string(5) "green" ["y"]=> string(6) "yellow" }

 

apart from the union operator, we can use the Equality (==),Identity (===),Inequality (!=), Inequality(<>), Non-identity(!==) in php arrays. These functionalities are as same as discussed above.

Happy Learning 🙂