# PHP Operators

PHP operators are a set of symbols that perform mathematical operations and other types of operations such as assignments, comparisons, string operations, etc.

These symbols are what we use to tell the PHP interpreter how to perform a calculation or processing on the data that we are working with.

There are 6 basic types of PHP operators. They are:

• Arithmetic - These operators carry out mathematical functions
• Assignment - These operators store values in variables
• Comparison - These operators compare values
• Increment/Decrement - These operators increment or decrement a number value
• Logical - These operators perform Boolean logic and returns true or false
• String - These operators join strings together

## PHP Operators - Arithmetic

The PHP operators which fall in the category of arithmetic operators are explained by example below:

```<?php

echo 8 + 4;  // performs addition
echo 8 - 4;  // performs subtraction
echo 8 * 4;  // performs multiplication
echo 8 / 4;  // performs division
echo 8 % 5;  // gives the modulus (remainder after division)

?>```

The results of the above calculations (on separate lines) are:

12
4
32
2
3

As we just saw above, PHP operators were used to perform basic mathematical functions.

## PHP Operators - Assignment

The basic assignment operation in PHP uses the equal sign ( = ) and is shown below:

```<?php

\$someVariable = "anything";
\$a = 3;
\$b = 4;
\$sum = \$a + \$b; // \$sum now contains the value 7

?>```

By using the PHP assignment operator we are able to store (or assign) a value to a variable.

## PHP Operators - Comparison

The comparison operators in PHP are shown below:

Symbol Name Result
== equal to true if a equals b, otherwise false
!= not equal to true if a does not equal b, otherwise false
=== identical to true only if a equals b and they are of the same type
!== not identical to true only if a does not equal b or they are not of the same type
< less than true if a is less than b, otherwise false
> greater than true if a is greater than b, otherwise false
<= less than or equal to true if a is less than or equal to b, otherwise false
>= greater than or equal to true if a is greater than or equal to b, otherwise false

If we were to output the result of a comparison operation directly we would get a 1 for true and no output for false.

The following comparisons will all return 1 because they are all true:

```<?php

\$a = 3;
\$b = 3;
\$c = 10;

// all of the following are true
echo (\$a == \$b);
echo (\$a != \$c);
echo (8 === 8);
echo ("8" !== 8);
echo (\$b < \$c);
echo (\$b <= \$b);
echo (\$c > \$b);
echo (\$c >= \$c);

?>```

The PHP comparison operators allow us to test a particular condition and will return a boolean result. We can then use this boolean result as part of a PHP If Statement for example.

## PHP Operators - Increment/Decrement

The following PHP operators increment (increase by 1) and decrement (decrease by 1) the value of a variable.

Look at the example below:

```<?php

\$a = 5;
\$a++;

echo \$a;
echo '<br />';

\$a--;

echo \$a;
?>```

Output:

6
5

As we just saw, the increment/decrement operators are used to increase or decrease the value of a numeric variable by 1.

## PHP Operators - Logical

PHP has a number of logical operators as shown below. These operators perform logical comparisons on variables and return Boolean values as results. The logical operators in PHP are:

Symbol Name Example Result
&& and a && b true if a and b are true, otherwise false
and and a and b true if a and b are true, otherwise false
|| or a || b true if a or b are true, otherwise false
or or a or b true if a or b are true, otherwise false
xor xor a xor b true only if a or b (but not both) are true
! not !a true if a is false; false if a is true

The PHP logical operators are used just like the PHP comparison operators as they return Boolean values as their result.

## PHP String Operator

This operator is simply a period ( . ) and is used to concatenate (join) strings. Look at this example:

```<?php

echo "Hello " . "there";

?>```

Output:

Hello there

## PHP Operator Precedence

Below is a table of PHP operator precedences. This list goes from highest precedence to lowest. Operators on the same line have a similar precedence. Remember that an operator with a higher precedence will always be evaluated before one with a lower precedence.

Of course, operations which are placed in brackets automatically have the highest precedence.

Operator Description
++ -- increment/decrement
(int) (float) (string) (array) (object) (bool) casting
! logical "not" (inverse)
* / % arithmetic operator
+ - . arithmetic and string
< <= > >= <> comparison
== != === !== comparison
&& logical "and"
|| logical "or"
= += -= *= /= .= %= assignment
and logical "and"
xor logical "xor"
or logical "or"

The above table is very important to know. Note that you might get incorrect results when you do calculations if you do not take note of the precedence of the operators you are using. Remember that you can use regular brackets () to enforce a particular precedence.

We hope this tutorial on PHP operators has been helpful.