Operators are used to perform operations or actions on variables. This can include assigning a value to a variable, performing addition with variables, comparing the value of variables, and to determine the status of a condition.
Arithmetic operators are used to perform basic mathematical operations. The following are arithmetic operators used in PHP:
| Operator | Description |
|---|---|
| + | performs addition |
| - | performs subtraction |
| * | performs multiplication |
| / | performs division |
| % | returns modulus (division remainder) |
| ++ | increments value |
| -- | decrements value |
<!DOCTYPE html PUBLIC "-//W3C//DTD/XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml11-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>A Web Page</title>
</head>
<body>
<p>
// The addition operator
<?php
$sum = 5 + 2;
$sum = 7;
$newsum = $sum + 4;
echo "The sum is " . $newsum
//The subtraction operator
$difference = $newsum - 2;
echo "The difference is " . $difference;
//The multiplication operator
$product = $difference * 3;
echo "The product is " . $product;
//The division operator
$quotient = $product / $difference;
echo "The quotient is " . $quotient;
//The Increment operator
$quotient++;
echo $quotient;
//The decrement operator
$quotient --
echo $quotient;
?>
</p>
</body>
</html>
Order of Operations
When an arithmetic expression is evaluated, there is a given order in which the operations are carried out. This order is called operator precedence. Multiplication and division take precedence (are done first) over addition and subtraction, moving from left to right through the expression. This order has important impact on whether or not you get the results expected. Consider the following declarations and assignments.
<?php
$num1 = 4;
$num2 = 5;
$num3 = 2;
$answer = $num1 * $num2 - $num3;
echo $answer;
?>
The resulting value would be 18. First, $num1 is multiplied by $num2 to get 20; then $num3 is subtracted from 20 to get 18. Suppose, however, that you really intended to first subtract $num3 from $num2 and then multiply by $num1 to get 12. The above expression will not work to produce this result because multiplication takes precedence over subtraction and is performed first.
Often, therefore, you need to override operator precedence and explicitly control the order of evaluation of an expression. This is done by using parentheses ( ) to govern the sequence in which arithmetic operations are carried out. In the above example, you would use the statement
<?php
$num1 = 4;
$num2 = 5;
$num3 = 2;
$answer = $num1 * ($num2 - $num3);
echo $answer;
?>
This time $answer is 12. $num3 is subtracted from $num2 to get 3. Next 3 is multiplied by $num1.
Assignment operators are used to change the value of the current variable with the value on the right of the operator. The following are Assignment operators used in PHP:
| Operator | Description |
|---|---|
| = | Assigns the value of the variable on the right to the value of the variable on the left |
| += | Adds the value on the left to the value on the right and assigns the result to the variable on the left. |
| -= | Subtracts the value on the left to the value on the right and assigns the result to the variable on the left. |
| *= | Multiplies the value on the left to the value on the right and assigns the result to the variable on the left. |
| /= | Divides the value on the left to the value on the right and assigns the result to the variable on the left. |
| %= | Divides the value on the left to the value on the right and assigns the remainder (modulus) to the variable on the left. |
| .= | The value on the left is concatenated (added on to) the value on the right and is assigned to the variable on the left |
Comparison operators are used to compare the value of variables. The following are Comparison operators used in PHP:
| Operator | Description |
|---|---|
| == | is equal to |
| != | is not equal to |
| > | is greater than |
| < | is less than |
| >= | is greater than or equal to |
| <= | is less than or equal to |
Logical operators allow you to determine the status of conditions. Depending on the condition of a variable different script actions can occur. Logical operators are used extensively with PHP decision structures. The following are Logical operators used in PHP:
| Operator | Description |
|---|---|
| && | AND |
| || | OR |
| ! | NOT |