# 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

 Operator Description Example Result + Addition \$a + \$b Sum of \$a and \$b – Subtraction \$a – \$b Subtraction of \$a and \$b * Multiplication \$a * \$b Multiplication of \$a and \$b % Modulus \$a % \$b Remainder of \$a divided by \$y / Division \$a / \$b Quotient 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.

 Operator Description Example Result = Assign \$a = \$b Assign 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.

 Operator Description Example Result == Equal to \$a == \$b True is value of \$a is equal to \$b != Not equal to \$a != \$b True if \$a is not equal to \$b === Identical \$a === \$b True if \$a and \$b are of same type and equal !== Not identical \$a !== \$b True if \$a and \$b are not of same type and equal <> Not equal to \$a <> \$b True if \$a is not equal to \$b < Less than \$a < \$b True if \$a is less than \$b <= Less or equal to \$a <= \$b True if \$a is less than or equal to \$b > Greater than \$a > \$b True if \$a is greater than \$b >= Greater than or equals to \$a >= \$b True 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.

 Operator Name Result ++\$a Pre increment Increment then execute \$a++ Post increment Execute then increment –\$a Pre decrement Decrement then execute \$a– Post decrement Execute 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

 Operator Name Example Result and And \$a and \$b True if both \$a and \$b are true && And \$a && \$b True if both \$a and \$b are true or Or \$a or \$b True if either \$a or \$b is true || Or \$a || \$b True if either \$a or \$b is true ! Not !\$a True if \$a is not true xor XOR \$a xor \$b True 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

 Operator Description Expample Result . Concatenation \$a.\$b Concatenate or combines \$a and \$b .= Concatenate and assign \$a .= \$b Concatenate \$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

 Operator Description Example Result == Equal to \$a == \$b True is value of \$a is equal to \$b != Not equal to \$a != \$b True if \$a is not equal to \$b === Identical \$a === \$b True if \$a and \$b are of same type and equal !== Not identical \$a !== \$b True if \$a and \$b are not of same type and equal <> Not equal to \$a <> \$b True if \$a is not equal to \$b + Union \$a + \$b Union 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
?>``````