Operators are symbols that is used to perform certain operations on variables and values using operators. For example, the subtraction () symbol is an operator that tells PHP to subtract two variables or values, while the greater-than ( > ) symbol is an operator that tells PHP to compare two values.
PHP divides the operators in the following form:

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





1.PHP Arithmetic operators

The arithmetic operators are used to perform arithmetical actions, like as addition, subtraction, multiplication etc. Let’s see a complete list of PHP’s arithmetic operators given below:

OperatorDescriptionExampleResult
+Addition$a + $bSum of $a and $b
-Subtraction$a - $bDifference of $a and $b.
*Multiplication$a * $bProduct of $a and $b.
/Division$a / $bQuotient of $a and $b
%Modulus$a % $bRemainder of $a divided by $b

The following example will show you these arithmetic operators in action:

Example

<?php
$x = 10;
$y = 4;
echo($a + $b); // 0utputs: 14

echo($a - $b); // 0utputs: 6

echo($a * $b); // 0utputs: 40

echo($a / $b); // 0utputs: 2.5

echo($a % $b); // 0utputs: 2
?>




2. PHP Assignment Operators

The PHP assignment operators are used to assign values to variables.The basic assignment operator in PHP is “=” .

OperatorDescriptionExampleSame As
=Assign$a = $b$a = $b
+=Add and assign$a += $b$a = $a + $b
-=Subtract and assign$a -= $b$a = $a - $b
*=Multiply and assign$a *= $b$a = $a * $b
/=Divide and assign quotient$a /= $b$a = $a / $b
%=Divide and assign modulus$a %= $b$a = $a % $b

The following example will show you all Assignment operators in action:

Example

<?php
$a = 100;
echo $a; // Outputs: 100
 
$a = 200;
$a += 30;
echo $a; // Outputs: 230
 
$a = 40;
$a -= 20;
echo $a; // Outputs: 20
 
$a = 10;
$a *= 25;
echo $a; // Outputs: 250
 
$a = 50;
$a /= 10;
echo $a; // Outputs: 5
 
$a = 100;
$a %= 15;
echo $a; // Outputs: 10
?>




3. PHP Comparison operators

The PHP comparison operators are used to compare two values (number or string):

OperatorNameExampleResult
==Equal$a == $bTrue if $a is equal to $b
===Identical$a === $bTrue if $a is equal to $b, and they are of the same type
!=Not equal$a != $bTrue if $a is not equal to $b
<>Not equal$a <> $bTrue if $a is not equal to $b
!==Not identical$a !== $bTrue if $a is not equal to $b, or they are not of the same type
<Less than$a < $bTrue if $a is less than $b
>Greater than$a > $bTrue if $a is greater than $b
>=Greater than or equal to$a >= $bTrue if $a is greater than or equal to $b
<=Less than or equal to$a <= $bTrue if $a is less than or equal to $b

The following example will show you Comparison operators in action:

Example

<?php
$a = 25;
$y = 35;
$z = "25";
var_dump($a == $c);  // Outputs: boolean true
var_dump($a === $c); // Outputs: boolean false
var_dump($a != $b);  // Outputs: boolean true
var_dump($a !== $c); // Outputs: boolean true
var_dump($a < $b);   // Outputs: boolean true
var_dump($a > $b);   // Outputs: boolean false
var_dump($a <= $b);  // Outputs: boolean true
var_dump($a >= $b);  // Outputs: boolean false
?>




4. PHP Increment/Decrement operators

The PHP increment/decrement operators are used to increment and decrement a variable’s value.

OperatorNameResults
++$aPre-incrementIncrements $a by one, then returns $a
$a++Post-incrementReturns $a, then increments $a by one
--$aPre-decrementDecrements $a by one, then returns $a
$a--Post-decrementReturns $a, then decrements $a by one

The following example will show you Increment/Decrement operators in action:

Example

<?php
$x = 10;
echo ++$x; // Outputs: 11
echo $x;   // Outputs: 11
 
$x = 10;
echo $x++; // Outputs: 10
echo $x;   // Outputs: 11
 
$x = 10;
echo --$x; // Outputs: 9
echo $x;   // Outputs: 9
 
$x = 10;
echo $x--; // Outputs: 10
echo $x;   // Outputs: 9
?>




5. PHP Logical Operators

The PHP logical operators are mostly used to combine conditional statements.

OperatorNameExampleResult
andAnd$a and $bTrue if both $a and $b are true
orOr$a or $bTrue if either $a or $b is true
xorXor$a xor $aTrue if either $a or $b is true, but not both
&&And$a && $bTrue if both $a and $b are true
||Or$a || $bTrue if either $$a or $b is true
!Not!$aTrue if $a is not true

The following example will show you Logical Operators operators in action:

Example

<?php
$year = 2019;
// Leap years are divisible by 400 or by 4 but not 100
if(($year % 400 == 0) || (($year % 100 != 0) && ($year % 4 == 0))){
    echo "$year is a leap year.";
} else{
    echo "$year is not a leap year.";
}
?>

Output:

2019 is not a leap year.




6. PHP String operators

In PHP String operators are follow two operators which are specifically designed for strings.

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

The following example will show you String Operators operators in action:

Example

<?php
$a = "Good";
$b = " Morning!";
echo $a . $b; // Outputs: Good Morning
 
$a .= $b;
echo $a; // Outputs: Good Morning!
?>

Output:

Good Morning
Good Morning




7. PHP Array operators

These array operators are used to compare arrays:

OperatorNameExampleResult
+Union$a + $bUnion of $a and $b
==Equality$a == $bTrue if $a and $b have the same key/value pairs
===Identity$a === $bTrue if $a and $b have the same key/value pairs in the same order and of the same types
!=Inequality$a != $bTrue if $a is not equal to $b
<>Inequality$a <> $bTrue if $a is not equal to $b
!==Non-identity$a !== $bTrue if $a is not identical to $b

The following example will show you Array Operators operators in action:

Example

<?php
$x = array("a" => "Red", "b" => "Green", "c" => "Blue");
$y = array("u" => "Yellow", "v" => "Orange", "w" => "Pink");
$z = $x + $y; // Union of $x and $y
var_dump($z);
var_dump($x == $y);   // Outputs: boolean false
var_dump($x === $y);  // Outputs: boolean false
var_dump($x != $y);   // Outputs: boolean true
var_dump($x <> $y);   // Outputs: boolean true
var_dump($x !== $y);  // Outputs: boolean true
?>

Pin It on Pinterest

Shares
Share This