Hello dear readers! welcome back to another section of my tutorial on PHP. In this tutorial guide, we will be discussing about the PHP Operators.

**What is an Operator?**

An operator can be explained as a symbol that mostly represents an action or process. Am going to be using the expression 6 + 5 equal to 11 as an example. Here 6 and 5 are called operands and + is the operator. PHP has support for the following type of operators.

- Arithmetic Operators

- Comparison Operators

- Logical Operators

- Assignment Operators

- Conditional Operator

Now let us look into these PHP supported operators one after the other.

RECOMMENDED: PHP Constants

**Arithmetic Operators**

The following are the Arithmetic Operators supported by PHP -

Assume variable A holds 10 and variable B holds 20, then -

Read Examples

Assume variable A holds 10 and variable B holds 20, then -

Read Examples

Operator | Description | Example |
---|---|---|

+ | Adds two operands | A + B will give 30 |

- | Subtracts second operand from the first | A - B will give -10 |

* | Multiply both operands | A * B will give 200 |

/ | Divide numerator by de-numerator | B / A will give 2 |

% | Modulus Operator and remainder of after an integer division | B % A will give 0 |

++ | Increment operator, increases integer value by one | A++ will give 11 |

-- | Decrement operator, decreases integer value by one | A-- will give 9 |

RECOMMENDED: PHP Variable and Data Types

**Comparison Operators**

The following are the Comparison Operators supported by PHP -

Assume variable A holds 10 and variable B holds 20, then -

Read Examples

Assume variable A holds 10 and variable B holds 20, then -

Read Examples

Operator | Description | Example |
---|---|---|

== | Checks if the value of two operands are equal or not, if yes then condition becomes true. | (A == B) is not true. |

!= | Checks if the value of two operands are equal or not, if values are not equal then condition becomes true. | (A != B) is true. |

> | Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. | (A > B) is not true. |

< | Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. | (A < B) is true. |

>= | Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. | (A >= B) is not true. |

<= | Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. | (A <= B) is true |

RECOMMENDED: PHP Basic Syntax

**Logical Operators**

The following are the Logical Operators supported by PHP -

Assume variable A holds 10 and variable B holds 20, then -

Read Examples

Assume variable A holds 10 and variable B holds 20, then -

Read Examples

Operator | Description | Example |
---|---|---|

and | Called Logical AND operator. If both the operands are true then condition becomes true. | (A and B) is true. |

or | Called Logical OR Operator. If any of the two operands are non zero then condition becomes true. | (A or B) is true. |

&& | Called Logical AND operator. If both the operands are non zero then condition becomes true. | (A && B) is true. |

|| | Called Logical OR Operator. If any of the two operands are non zero then condition becomes true. | (A || B) is true. |

! | Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. | !(A && B) is false. |

RECOMMENDED: How to Install PHP on Mac OS X

**Assignment Operators**

Operator | Description | Example |
---|---|---|

= | Simple assignment operator, Assigns values from right side operands to left side operand | C = A + B will assign value of A + B into C |

+= | Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand | C += A is equivalent to C = C + A |

-= | Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand | C -= A is equivalent to C = C - A |

*= | Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand | C *= A is equivalent to C = C * A |

/= | Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand | C /= A is equivalent to C = C / A |

%= | Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand | C %= A is equivalent to C = C % A |

RECOMMENDED: PHP Function Parameters

**Conditional (Ternary) Operator**

There is one last operator called conditional operator. This first evaluates an expression for a true or false value, and executes one of the two given statements based on the result of the evaluation.

Read Examples

Read Examples

Operator | Description | Example |
---|---|---|

? : | Conditional Expression | If Condition is true ? Then value X : Otherwise value Y |

**Operators Categories**

All the operators that have been studied above can be grouped into the following categories -

- The unary prefix operators - which precede only a single operand.

- The binary operators - which take two operands and then perform series of arithmetic and logic operations.

- The ternary operator - which collects three operands and evaluates either the second or third expression, based on the evaluation of the first expression.

- The assignment operators - which assigns a value to a variable.

RECOMMENDED: PHP Local Variables

**Operator Precedence and Associativity**

The precedence of PHP operators decides the grouping of terms in an expression. It affects how an expression is evaluated.

Certain operators have a higher precedence than some others; for example, a multiplication operator has higher precedence than the addition operator -

For example x = 10 + 3 * 2; Here x is assigned the value 16, and not 26 because the operator * has a higher precedence than + so it first get multiplied with 3*2 and then adds into 10.

Here operators with the highest precedence appear at the top of the table, and those with lowest appear at the bottom. Within an expression, the higher precedence operators will be evaluated first.

Certain operators have a higher precedence than some others; for example, a multiplication operator has higher precedence than the addition operator -

For example x = 10 + 3 * 2; Here x is assigned the value 16, and not 26 because the operator * has a higher precedence than + so it first get multiplied with 3*2 and then adds into 10.

Here operators with the highest precedence appear at the top of the table, and those with lowest appear at the bottom. Within an expression, the higher precedence operators will be evaluated first.

Category | Operator | Associativity |
---|---|---|

Unary | ! ++ -- | Right to left |

Multiplicative | * / % | Left to right |

Additive | + - | Left to right |

Relational | < <= > >= | Left to right |

Equality | == != | Left to right |

Logical AND | && | Left to right |

Logical OR | || | Left to right |

Conditional | ?: | Right to left |

Assignment | = += -= *= /= %= | Right to left |

RECOMMENDED: How to Install PHP on Windows with Apache

Alright guys! This is where we are rounding up for this tutorial post. In my next tutorial post, we are going to be discussing about the Arithmetic Operators.

Feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.

Feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.

## 0 comments:

## Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.