PHP Operators
Here are most types of PHP Operators: Assignment Operators, Arithmetic Operators, Comparison Operators, and String Operators.
Assignment Operators
We have already met this type. This is where we set a variable equal to something or make a variable equal to another variable.
Arithmetic Operators
| English | Operator | Example |
| Addition | + | 1+1 |
| Subtraction | - | 5-3 |
| Multiplication | * | 5*2 |
| Division | / | 10/2 |
An example of the use of Arithmetic operators is shown below:
<?php $add = 1+1; $subtract = 5-3; $multiply = 5*2; $divide = 10/2; print “Addition Answer: ” . $add . “<br>”; print “Subtraction Answer: ” . $subtract. “<br>”; print “Multiplication Answer: ” . $multiply. “<br>”; print “Addition Answer: ” . $divide. “<br>”; ?>
The output of the above code is:
Addition Answer: 2
Subtraction Answer: 2
Multiplication Answer: 10
Addition Answer: 5
This showing that the server is carrying out the calculations, using our arithmetic operators. As well as the above, you could also use these operators in conjunction with each other like so:
<?php $quantity = 4; $value = 5; $total = $quantity * $value; print $total; ?>
The output from the above is 20.
Comparison Operators
Comparison Operators are used to check the relationship between variables/values. These are used inside statements (such as if statements) to check whether the result is true or false. Listed are some PHP comparison operators:
We will assume, for this example, that $a = 2 and $b = 3.
| English | Operator | Example | Result |
| Equal to | == | $a == $b | False |
| Not equal to | != | $a != $b | True |
| Less than | < | $a < $b | True |
| Greater than | > | $a > $b | False |
| Less than or equal to | <= | $a <= $b | True |
| Greater than or equal to | >= | $a >= $b | False |
Please note the two equal signs in the equal to example – this is a common beginners’ mistake.
We will go into further examples of Comparison Operators in the next section on PHP If Statements.
String Operators
We have already met String Operators, please read prior section named Concatenation.