Operators execute different type of functions on the value or variables. PHP has so many operators that are why we have classified them.

• Arithmetic Operators

• Assignment Operators

• String Operators

• Comparison Operators

• Combination Arithmetic & Assignment Operators

Arithmetic Operators:-

Operator English Example

+ Addition 8 + 4

– Subtraction 5 – 2

* Multiplication 4 * 3

/ Division 18 / 3

% Modulus 44 % 10

Code of PHP:

$subtraction = 5 - 2;

$multiplication = 4 * 3;

$division = 18 / 3;

$modulus = 44 % 10;

echo "Perform addition: 8 + 4 = ".$addition."<br />";

echo "Perform subtraction: 5 - 2 = ".$subtraction."<br />";

echo "Perform multiplication: 4 * 3 = ".$multiplication."<br />";

echo "Perform division: 18 / 3 = ".$division."<br />";

echo "Perform modulus: 44 % 10 = " . $modulus

Exhibit

Perform subtraction: 5 - 2 = 3

Perform multiplication: 4 * 3 = 12

Perform division: 18 / 3 = 6

Perform modulus: 44 % 10 = 4

Assignment Operators:-

The function of Assignment operator is to make the value of one variable equivalent to the other variable. The sign of the operator is “=”.

For instance:

$other_var = $my_var;

Both the variables have same value ‘5’.

String Operators

In PHP, “.” has a specific function which is to adjoin 2 strings collectively.

Code of PHP:

$another_string = " James";

$new_string = $a_string . $another_string;

echo $new_string . "!";

Exhibit

Comparison Operators

Comparison operators compare the variables and values. It is just like if statement.

For example: $x= 9 and $y = 7

Operator English Example Result

== Equal To $x == $y false

!= Not Equal To $x != $y true

< Less Than $x < $y False
> Greater Than $x > $y True

<= Less Than or Equal To $x <= $y False
>= Greater Than or Equal To $x >= $y True

Combination Arithmetic and Assignment Operators

It is frequent in programs that we use a counter for making increase in our values.

• $counter = $counter + 2;

We can also write like this:

• $counter += 2;

This function is performed by combination assignment and arithmetic operator.

Operator English Example Equivalent Operation

+= Plus Equals $x += 2; $x = $x + 2;

-= Minus Equals $x -= 4; $x = $x – 4;

*= Multiply Equals $x *= 3; $x = $x * 3;

/= Divide Equals $x /= 2; $x = $x / 2;

%= Modulo Equals $x %= 5; $x = $x % 5;

.= Concatenate Equals $my_str.=”hello”; $my_str = $my_str . “hello”;

Rise in pre/post and fall in pre/post:

You can also use “++” operator for addition.

? $x++; is equal to $x += 1; it can be $x= $x+1;

For subtraction, “–” is used.

? $x–; it is equal to $x-= 1; it can be $x=$x- 1;

Code of PHP:

echo "The given value of x in connection to post-plusplus = " . $x++;

echo "<br /> The given value of x following to post-plusplus " . $x;

$x = 6;

echo "<br />The given value of x in connection to pre-plusplus = " . ++$x;

echo "<br /> The given value of x following to pre-plusplus " . $x;

Exhibit:

The given value of x following to post-plusplus = 7

The given value of x in connection to pre-plusplus = 7

The given value of x following to pre-plusplus = 7