PHP Operators


August 23, 2021, Learn eTutorial
1521

In this PHP tutorial, you will learn all about the operators in PHP. We will discuss in detail the types of operators.

What is meant by PHP operators?

PHP operators are the symbols that are used to perform the operation on the variable or value and evaluate the result. These values of variables are known as the operands. Statements that contain any of the operator and the operands are known as the expression. 

The operators can be categorised into 3 

  1. Unary Operator:These are the operators which works on a single operand.
  2. Binary Operator:These are the operators which works on a two operand.
  3. Ternary Operator:These are the operators which works on a three operand.

Different types of operators used in PHP

  1. Arithmetic Operator
  2. Comparison Operator
  3. Increment & Decrement Operator
  4. Assignment Operator
  5. Logical Operator
  6. String Operator
  7. Array Operator
  8. Conditional or Ternary Operator
  9. Type Operator
  10. Execution Operator
  11. Error Control Operator
  12. Spaceship Operator
  13. Bitwise Operator 

PHP Arithmetic Operator

The arithmetic operators are used to perform the basic arithmetic operations such as addition, subtraction, multiplication, etc. 

Operator

Name

Description

+

Addition

It is used to find the sum of the operands

-

Subtraction

It is used to find the difference of the operands

*

Multiplication

It is used to find the product of the operands

/

Division

It is used to find the quotient of the operands

%

Modulus

It is used to find the reminder of the operands

**

Exponentiation

It is used to find the second operand power of the first operands


$a = 10;
$b = 4;
$sum = $a + $b; //Addition operator
$dif = $a - $b; //Subtraction operator
$pro = $a * $b; //Multiplication operator
$div = $a / $b; //Division operator
$rem = $a % $b; //Modulus operator
$pow = $a ** $b; //Exponentiation operator
echo $sum . "\n";
echo $dif . "\n";
echo $pro . "\n";
echo $div . "\n";
echo $rem . "\n";
echo $pow . "\n";

Output:


14
6
40
2.5
2
10000

PHP Comparison Operator

The comparison operators are used to two values (operands) which can be strings or numbers.

Operator Name Description

==

Equal

It will return true if the both the operands values are equal

===

Identical

It will return true if the both the operands values and their data-types are equal

!=

Not Equal

It will return true if the both the operands values are not equal

<> 

Not Equal

It will return true if the both the operands values are not equal

!==

Not Identical

It will return true if the both the operands values and their data-types are not equal

Greater than

It will return true if the operand on the left-hand side is greater than the operand on the right-hand side

Less than

It will return true if the operand on the left-hand side is lesser than the operand on the right-hand side

>=

Greater than equal to

It will return true if the operand on the left-hand side is greater than or equal to the operand on the right-hand side

<=

Less than equal to

It will return true if the operand on the left-hand side is less than or equal to the operand on the right-hand side


$a = 10;
$b = 4;
var_dump($a == $b) . "\n";      // Equal operator
var_dump($a === $b) . "\n";     // Identical operator
var_dump($a != $b) . "\n";      // Not Equal operator
var_dump($a <> $b) . "\n";      // Not Equal operator
var_dump($a !== $b) . "\n";     // Not Identical operator
var_dump($a > $b) . "\n";       // Greater than operator
var_dump($a < $b) . "\n";       // Less than operator
var_dump($a >= $b) . "\n";      // Greater than equal to operator
var_dump($a <= $b) . "\n";      // Less than equal to operator


Output:


bool(false)
bool(false)
bool(true)
bool(true)
bool(true)
bool(true)
bool(false)
bool(true)
bool(false)

PHP Increment & Decrement Operator

The increment operator is used to increment the value of the operand by 1

The decrement operator is used to decrement the value of the operand by 1

Operator Name Description
++$a Pre-increment It will increment the value of the operand at first and then return the value
$a++ Post-increment It will return the value at first and then increment the value of the operand
--$a Pre-decrement It will decrement the value of the operand at first and then return the value
$a++ Post-decrement It will return the value at first and then decrement the value of the operand

$a = 10;
$b = 10;
$c = 10;
$d = 10;
echo ++$a . "\n";   // Pre-increment operator
echo $b++ . "\n";   // Post-increment operator
echo --$c . "\n";   // Pre-decrement operator
echo $d-- . "\n";   // Post-decrement operator

Output:


11
10
9
10

PHP Assignment Operator

The assignment operator is used to assign the value of the operand in the right-hand side to the operand in the left-hand side.

Operator Name Description
= Assign It is used to assign the value of the operand in the right-hand side to the operand in the left-hand side
+= Add then assign It first adds the value of two operands and then assign the result to the operand in left-hand side
-= Subtract then assign It first subtracts the value of two operands and then assign the result to the operand in left-hand side
*= Multiply then assign It first multiplies the value of two operands and then assign the result to the operand in left-hand side
/= Divide then assign(quotient) It first divides the value of two operands and then assign the quotient to the operand in left-hand side
%= Divide then assign(reminder) It first divides the value of two operands and then assign the reminder to the operand in left-hand side

$a = 10;
$b = 10;
$c = 30;
$d = 8;
$e = 9;
$f = 4;

$x = $a;        // assign operator
echo $x . "\n";
$b += $a;       // Add then assign operator
echo $b . "\n";
$b -= $a;       // Subtract then assign operator
echo $b . "\n";
$d *= $f;       // Multiply then assign operator
echo $d . "\n";
$b /= $a;       // Divide then assign(quotient) operator
echo $b . "\n";
$c %= $e;       // Divide then assign(reminder) operator
echo $c . "\n";


Output:


10
20
10
32
1
3

PHP Logical Operator

In PHP to evaluate two expressions or statements, logic operators are used. The three basic logic operators are and, or and not. These operators control program flow in a boolean context similar to comparison operators. Boolean values are either True(1) or False(0).

Operator Name Description
and And It will return true if both hand sides values are true
or Or It will return true if any hand sides values are true
xor Xor It will return true if any hand sides values are true but not both are true
&& And It will return true if both hand sides values are true
|| Or It will return true if any hand sides values are true
! Not It is a unary operator it will return true if the operand value if false and return false if the operand value is true

$a = 10;
$b = 20;
var_dump($a == 10 and $b == 20) . "\n";         //And  operator
var_dump($a == 10 or $b == 15) . "\n";          //Or  operator
var_dump($a == 20 xor $b == 10) . "\n";         //Xor  operator
var_dump($a == 18 && $b == 20) . "\n";          //And  operator
var_dump($a == 10 || $b == 20) . "\n";          //Or  operator
var_dump(!($a == 15)) . "\n";                   //Not  operator


Output:


bool(true)
bool(true)
bool(false)
bool(false)
bool(true)
bool(true)

PHP String Operator

String operators in PHP are used to perform the operation on strings.

Operator Name Description
. Concatenation It is used to concatenate two strings
.= Concatenate and assign It is used to concatenate the both sides and assign the value to the left-hand side operand

$a = "learnE";
$b = "tutorials.com";
echo $a . $b;               // Concatenation operator
echo "\n";
$a .= $b;                   // Concatenate and assign operator
echo $a;


Output:


learnEtutorials.com
learnEtutorials.com

PHP Array Operator

PHP array operator are used to perform operations on the arrays. They are mostly used for the comparison of the array.

Operator Name Description
+ Union It is used to get the union of the two arrays
== Equality It will return true if both the arrays are equal
=== Identical It will return true if both the values of the array and their data-types are equal
!= Inequality It will return true if both the arrays are not equal
<>  Inequality It will return true if both the arrays are not equal
!== Non-identical It will return true if both the values of the array and their data-types are not equal

$a = array("a" => "PHP", "b" => "Python");
$b = array("c" => "C++", "d" => "Java");
print_r($a + $b);       // Union operator
var_dump($a == $b);     // Equality operator
var_dump($a === $b);     // Identical operator
var_dump($a != $b);     // Inequality operator
var_dump($a <> $b);     // Inequality operator
var_dump($a !== $b);     // Non-identical operator

Output:


Array
(
    [a] => PHP
    [b] => Python
    [c] => C++
    [d] => Java
)
bool(false)
bool(false)
bool(true)
bool(true)
bool(true)

PHP Conditional or Ternary Operator

PHP conditional assignment operators are used to set values based on conditions.

Operator Name Description
?: Ternary It is similar to the if statement if. It checks the first expression if it is true, it returns the value of the second expression otherwise it returns the value of the third expression
?? Null coalescing It checks for two expressions if the first expression is null or doesn’t exists then it will return the second expression, and if it do exists  it will return first expression

$age = 23;
echo $age > 18 ? "can vote" : "cant vote";   // Ternary operator
echo "\n";
echo  $age ?? "no age given";       // Null coalescing operator
echo "\n";
echo $gender ?? "gender not given";       // Null coalescing operator

Output:


can vote
23
gender not given

PHP Type Operator

The type operator in PHP ‘instanceof’ is used to determine whether an object, its parent and its derived class are the same type or not. Generally, this operator identifies the class an object belongs to.


class Abc
{
}
class Xyz
{
}
$value = new Abc();

var_dump($value instanceof Abc);
var_dump($value instanceof Xyz);

Output:


bool(true) 
bool(false)

PHP Execution Operator

In PHP the execution operator is used to perform the shell command.

Operator Name Description
`` backticks It is used to execute the shell command in PHP

$path = `dir`;
echo $path;
// to find the path of the file

Output:


Volume in drive C is Acer
 Volume Serial Number is 16BF-2C7E

 Directory of C:\XAMPP FOLDER\htdocs\operators

03-11-2021  01:33 AM    
. 03-11-2021 01:33 AM .. 03-11-2021 02:39 AM 38 comp.php 1 File(s) 38 bytes 2 Dir(s) 371,202,158,592 bytes free

PHP Error Control Operator

The error control operator is used to ignore the error message formed by the expression. We use the symbol @ (at) as the error control operator.

Example before using @


$val = 76 / 0;
echo "Welcome to learnetutorials.com";

Output:


Warning: Division by zero in C:\XAMPP FOLDER\htdocs\MYPC\operators\comp.php on line 3
Welcome to learnetutorials.com

Example using @


@$val = 76 / 0;               // while trying to divide a number by 0 it gives error. By using the @ operator we can ignore the error message
echo "Welcome to learnetutorials.com";


Output:


Welcome to learnetutorials.com

PHP Spaceship Operator

The spaceship operator is more like the comparison operator. It will compare the operands and the value of the first operand is greater than the second operand it will return 1 , and if both the operands are same then it will return 0 otherwise it will return -1.

Example using @


echo 10 <=> 5;
echo "\n";
echo 5 <=> 10;
echo "\n";
echo 5 <=> 5;

Output:


1
-1
0

PHP Bitwise Operator

The bitwise operators are used to perform bit-level operations on operands. These operators allow the evaluation and manipulation of specific bits within the integer.

Operator Name Description
& And If bit 1 is in both the operand then it will return 1, otherwise it will return 0
| Or(Inclusive or) If bit 1 is in any of the operand, then it will return 1, otherwise it will return 0
^ Xor(Exclusive or) If 1 or 0 in both the operands then it will be 0 otherwise it will be 1
~ Not If it is 1 then it returns 0, otherwise it returns 1
<<  Shift left It shifts the value from right to left
>>  Shift right It shifts the value from left to right

Example using @


$a = 7;
$b = 5;

echo $a & $b;           // bitwise and operator
echo "\n";
echo $a | $b;           // bitwise or operator
echo "\n";
echo $a ^ $b;           // bitwise xor operator
echo "\n";
echo ~$b;               // bitwise not operator
echo "\n";
echo $a << $b;          // bitwise shift left operator
echo "\n";
echo $a >> $b;          // bitwise shift right operator
echo "\n";

Output:


5
7
2
-6
224
0