Register Now

Login

Lost Password

Enter your email to reset your password.

What is Operators in PHP?

An operator is a symbol that takes one or more values (operands) and yields another value.
For example: 4 + 5 = 9. Here symbol “+” is an operator which takes 4 and 5 as values(operands) for operation addition and result 9 as output.

PHP language supports operators in the following groups:

  1. Arithmetic operators
  2. Assignment operators
  3. Comparison operators
  4. Increment/Decrement operators
  5. Logical operators
  6. String operators
  7. Array operators
  8. Conditional assignment operators

1. PHP Arithmetic Operators

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

OperatorNameExampleResult
+Addition$x + $ySum of $x & $y
Substraction$x – $yDifference of $x & $y
*Multiplication$x * $yProduct of $x & $y
/Division$x / $yQuotient of $x & $y
%Modulus$x % $yRemainder of $x divided by $y

<?php

$x = 14;
$y = 4;

//Addition
echo($x + $y); // 0utputs: 18

//Substraction
echo($x - $y); // 0utputs: 10

//Multiplication
echo($x * $y); // 0utputs: 56

//Division
echo($x / $y); // 0utputs: 3

//Modulus
echo($x % $y); // 0utputs: 2

?>

2. PHP Assignment Operators

The Assignment operators are used to assign values to different variable.

OperatorsDescriptionExample
=Assigns values from right side to left side operandx=y
+=Add & Assignment operator. It adds right operand to the left operand & assigns result to the left operand.x +=y
-=Substract & Assignment operator. It substract right operand to the left operand & assigns result to the left operand.x -=y
*=Multiply & assignment operator. It multiplies the right operand to the left operand & assigns result to the left operand.x *=y
/=Divide & assignment operator. It divides left operand with the right operand & assigns the result to the left operand.x /=y
%=Modulus & assignment operator. It makes modulus using two operands & assigns the value to the left operands.x %=y

3. PHP Comparison Operators

The PHP Comparison Operators are used to compare two values.

OperatorsNameExampleDescription
==Equal$x==$yReturns true if $x is equal to $y
===Identical$x===$yReturns true if $x is equal and same type of $y.
!=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 or are not the same types to $y
>Greater than$x>$yReturns true if the $x is greater than $y
<Less than$x<$yReturns true if the $x is less than $y
>=Greater than or equal to $x >= $yReturns true if the $x is greater than or equal to $y
<=Less than or equal to $x <= $yReturns true if the $x is less than or equal to $y

4. PHP Increment / Decrement Operators

The PHP increment operators are used to increment a variable’s value. And PHP decrement operators are used to decrements a variable’s value.

OperatorsNameDescription
++$xPre-incrementIncrements $x by one, and returns $x.
$x++Post-incrementReturns $x first, then increment $x by one.
– -$xPre-decrementDecrements $x by one and returns $x.
$x- –Post-decrementReturns $x first then decrements $x by one.

5. PHP Logical Operators

Logical operators are very important as it comes use in real world program. The PHP logical operators are used to combine conditional statements.

OperatorNameExampleResult
&&And$x && $yReturns true if both $x & $y are true.
||Or$x || $yReturns true if any of $x, $y is true.
andAnd$x and $yReturns true if both $x & $y are true.
orOr$x or $yReturns true if any of $x, $y is true.
xorXor$x Xor $yReturns true if either $x or $y is true, but not both.
!Not!$x<dsfReturns true if $x is not true.

6. PHP String Operators

There are two string operators. The first is String Operator(‘.’) and the second one is concatenating assignment operator (‘.=’).

  • . Concatenation
  • .= Combined Concatenation

Let’s us understand with example –

Concatenation Example (‘.’) –

&lt;?php
$firstname = "Share";
$lastname = "Query";
echo $firstname . " ". $lastname ;
?&gt;
Output: Share Query

Combined Concatenation Example (‘.=’) –

&lt;?php
$mystring = 'Welcome to ';
$mystring .= 'Share ';
$mystring .= 'Query';
echo $mystring;
?&gt;
Output: Welcome to Share Query

7. PHP Array Operators

The PHP array operators are used to compare with arrays.

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

8. PHP Conditional Assignment Operators

The PHP conditional assignment operators are used to set a value depending on conditions/expressions.

There are two conditional assignment operators in PHP –

  1. Ternary (‘?:’) –
  2. Null Coalescing (‘??’) –

Ternary Operator – Ternary operator actually means “an operator which involves on three operands.

The Ternary Operator will result lefthand operand when the condition evaluates to true. And the righthand operand is used for default or false values.

$result = $condition ? $condition : 'default';

Example –

&lt;?php

//if empty($user) = TRUE
$status = (empty($user)) ? "Please Login First" : "Logged In";
echo $status; 
//Output: Please Login First

//if empty($user) = FALSE:
$user = "Kumud";
$status = (empty($user)) ? "Please Login First" : "Logged In";
echo $status;
//Output: Logged In

?&gt;

Null Coalescing – Null Coalescing operator (??) is used to replace the ternary operator with the help of isset() function. The Null coalescing operator returns its first operand if it exists & it is not NULL otherwise it will return the second operand.

Example –

&lt;?php

//If user is not logged in
$status = $_GET['username'] ?? "Please Login" ;
echo $status;

//Chaining Operation using Null Coalescing
$status = $_GET['username'] ?? $_POST['username'] ?? 'Please Login';
echo $status;

?&gt;