PHP Tutorials: Operators

Those who are familiar with PHP knows about Operators. But few of you are new to PHP or any programming language. In this tutorial I am going to explain you about operators.

What is an operator?

Operator is a symbol which tells the PHP processor to do an operation or you can say an action. Few of the operators are very known to all of us i.e. + for adding values, for minus etc. There are different type of operators in PHP.

  • Arithmetic Operators
  • Assignment Operators
  • Comparison Operators
  • Increment and Decrements Operators
  • Logical Operators
  • String Operators
  • Array Operators
  • Spaceship Operators

Arithmetic Operators

These operators are used for common mathematics operations

OperatorDescriptionExampleResult
+Addition$a + $bSum of $a and $b
Subtraction$a – $bSubtraction of $a and $b
*Multiplication$a * $bMultiplication of $a and $b
%Modulus$a % $bRemainder of $a divided by $y
/Division$a / $bQuotient of $a and $b
<?php
     $a = 10;
     $b = 4;
     echo $a + $b;     //14 
     echo $a - $b;     //6
     echo $a * $b;     //40
     echo $a % $b;     //2
     echo $a / $b;     //2.5

?>

Assignment Operators

These operators are used to store or assign value to a variable.

OperatorDescriptionExampleResult
=Assign$a = $bAssign value of $b to $a
+=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$a /= $b$a = $a / $b
%=Modulus and assign$a %= $b$a = $a % $b
<?php
     $a = 10;
     echo $a;     //10
     $b = 4;

     $a =+ $b;
     echo $a;      //14
     $a -= $b;
     echo $a;     //10     
?>

Comparision Operators

Comparision operators are used to compare 2 values. Comparison operators has only one possible values out of two True or False. If the comparison matches it return true or it will return false. Here is a list of comparison operators.

OperatorDescriptionExampleResult
==Equal to$a == $bTrue is value of $a is equal to $b
!=Not equal to$a != $bTrue if $a is not equal to $b
===Identical$a === $bTrue if $a and $b are of same type and equal
!==Not identical$a !== $bTrue if $a and $b are not of same type and equal
<>Not equal to $a <> $bTrue if $a is not equal to $b
<Less than$a < $b True if $a is less than $b
<=Less or equal to$a <= $bTrue if $a is less than or equal to $b
>Greater than$a > $bTrue if $a is greater than $b
>=Greater than or equals to$a >= $bTrue if $a is greater or equal to $b
<?php
	$a = 18;
	$b = 28;
	$c = "18";
	
	var_dump($a == $c);  // boolean true
	var_dump($a != $b);  // boolean true
	var_dump($a <> $b);  // boolean true

	var_dump($a === $c); // boolean false
	var_dump($a !== $c); // boolean true

	var_dump($a < $b);   // boolean true
	var_dump($a <= $b);  // boolean true

	var_dump($a > $b);   // boolean false
	var_dump($a >= $b);  // boolean false
?>

Incrementing and Decrementing Operators

Increment and Decrement operators are used to increase or decrease the value by 1. Most of the programmer finds these operators very complicated but they are innocent piece of code. Both has pre and post operations i.e. Pre increment and Post increment, pre decrement and Post decrement.

A pre-increment value executed before the statement ++$a. If we do

<?php
      $a = 10;
      echo ++$a; 
?>

The output of the above will be 11. Now you will see here that the increment gets done before the echo statement gets executed. The opposite applies to the post-increment. In post-increment case, the statement gets executed first and then it gets incremented.

<?php
      $a = 10;
      echo $a++;
      echo $a; 
?>

The output of the first echo will be 10 and the next will be 11. The value of $a gets incremented after the first echo.

The same rule applies to the decrement.

OperatorNameResult
++$aPre incrementIncrement then execute
$a++Post incrementExecute then increment
–$aPre decrementDecrement then execute
$a–Post decrementExecute then decrement.

Logical Operators

Logical operators are used to combining conditional operators. There will be 1 possible result out of 2. True or false. Here are logical operators of PHP

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

String Operators

String operators are very common and will be used frequently. One of the 2 operators are explained in our previous tutorial PHP: String & String Operations

OperatorDescriptionExpampleResult
.Concatenation$a.$bConcatenate or combines $a and $b
.=Concatenate and assign$a .= $bConcatenate $a and $b then assign to $a. $a = $a.$b
<?php
	$a = "John";
	$b = " Doe";
	echo $a . $b; // John Doe
	 
	$a .= $b;
	echo $a; // John Doe
?>

Array Operators

Array operators are very similar to Logical operators. There is a very little or no difference

OperatorDescriptionExampleResult
==Equal to$a == $bTrue is value of $a is equal to $b
!=Not equal to$a != $bTrue if $a is not equal to $b
===Identical$a === $bTrue if $a and $b are of same type and equal
!==Not identical$a !== $bTrue if $a and $b are not of same type and equal
<>Not equal to $a <> $bTrue if $a is not equal to $b
+Union$a + $bUnion of $a and $b
<?php
	$a = array("Sparrow","Parrot","Pigeon");
	$b = array("Swan","Duck","Eagle");
	
	
	var_dump($a == $b);   // boolean false
	var_dump($a === $b);  // boolean false
	var_dump($a != $b);   // boolean true
	var_dump($a <> $b);   // boolean true
	var_dump($a !== $b);  // boolean true

	$c = $a + $b; // Union of $a and $b
	var_dump($c);
?>

Spaceship Operators

With the enhancement of PHP, it has introduced some more operators for comparision. It will be easy to understand these operators if you understood the comparison operators described above. The spaceship operator is a 3 way comparision operator. It return 1 values out 3 possible values 0, 1 or -1. it is dennoted by <=>.

The spaceship operator returns 0 if both variables are equal. 1 if the left is greater and -1 if the right is greater.

<?php

	echo 10 <=> 10; // 0
	echo 10 <=> 20; // -1
	echo 20 <=> 10; // 1
?>

Try some examples and share your comments here.

2 thoughts on “PHP Tutorials: Operators

Leave a Reply

Your email address will not be published. Required fields are marked *