An operator is a symbol that works on operands. For example x = y + z. In this example, x, y and z are operands while = and + are operators. We would now examine the following operators supported by PHP.
- Arithmetic Operators
- Comparison Operators
- Logical (or Relational) Operators
- Assignment Operators
- Conditional (or ternary) Operators
1. Arithmetic Operators
These are operators used to perform arithmetic operation. For example if x is 10 and y is 20, then the following
| Operator | Description | Example |
|---|---|---|
| + | Adds two operands | x + B will give 30 |
| – | Subtracts second operand from the first | x – B will give -10 |
| * | Multiply the two operands | x * B will give 200 |
| / | Divide numerator(top) by de-numerator(down) | y / x will give 2 |
| % | Modulus Operator and remainder of after an integer division | y % x will give 0 |
| ++ | Increment operator, increases integer value by one | x++ will give 11 |
| — | Decrement operator, decreases integer value by one | x– will give 9 |
2. Comparison Operators
These are operators used to compare two numbers. For example, is x is 10 and y is 20, then the following holds
| Operator | Description | Example |
|---|---|---|
| == | Tests if the value of two operands are equal or not, if they are, then condition becomes true. | (x == y) is not true. |
| != | Tests if the value of two operands are equal or not, if values are not equal then condition becomes true. | (x != y) is true. |
| > | Tests if the value of left operand is greater than the value of right operand, if yes then condition becomes true. | (x > y) is not true. |
| < | Tests if the value of left operand is less than the value of right operand, if yes then condition becomes true. | (x < y) is true. |
| >= | Tests if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. | (x >= y) is not true. |
| <= | Tests if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. | (x <= y) is true. |
3. Logical (or Relational) Operators
These operators are used to test the logical values of the operands. They return either true or false values. For example, if x is 10 and y is 20, then the following holds
| Operator | Description | Example |
|---|---|---|
| and | Also known as Logical AND operator. If both operands are true then condition becomes true. | (x and y) is true. |
| or | Also known as Logical OR Operator. If any of the operands are non zero then condition becomes true. | (x or y) is true. |
| && | Also known as Logical AND operator. If both operands are non zero then condition becomes true. | (x && y) is true. |
| || | Also known as Logical OR Operator. If any of the operands are non zero then condition becomes true. | (x || y) is true. |
| ! | Also known as Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. | !(x && y) is false. |
4. Assignment Operators
These are used to assign values to variables. Examples are given in the table below:
| Operator | Description | Example |
|---|---|---|
| = | Simple assignment operator, Assigns values from right side operands to left side operand | z = x + B will assign value of x + y into z |
| += | Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand | z += x is equivalent to z = z + x |
| -= | Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand | z -= x is equivalent to z = z – x |
| *= | Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand | z *= x is equivalent to z = z * x |
| /= | Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand | z /= x is equivalent to z = z / x |
| %= | Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand | z %= x is equivalent to z = z % x |
5. Conditional (or ternary) Operators
This is an important operator to know. This is the only operator that works with three operand. It checks the value of the first operand, if it is true then the whole expression becomes the second operand, if false, the whole expression becomes the third operand.
Example of ternary operator is given below:
<?php $x = 10; $y = 20; /* If condition is true then assign value of x to result else y */ $result = ($x > $y ) ? $x :$y; echo "TEST_1 : Value of result is $result<br/>"; /* If condition is true then assign value of x to result else y */ $result = ($x < $y ) ? $x :$y; echo "TEST_2 : Value of result is $result<br/>"; ?>