Hello folks! welcome back to a new edition of our tutorial on PHP. In this tutorial guide, we are going to be studying about the PHP Operators.
What is an Operator?
An operator is a symbol that represents an action or process. We are going to use the expression 6 + 5 equal to 11 as an example. Here 6 and 5 are called operands and then + is the operator. PHP supports 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.
READ: 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 |
Comparison Operators
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 |
READ: PHP Basic Syntax
Logical Operators
Following below 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. |
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 |
READ: 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 then 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 above operators can be grouped into the following categories -
- Unary prefix operators - which precede only a single operand.
- The binary operators - which take two operands and then performs series of arithmetic and logic operations.
- The conditional operator - which takes three operands and evaluate either the second or third expression, depending on evaluation of the first expression.
- Assignment operators - which assigns a value to a variable.
READ: PHP Local Variables
Operator Precedence and Associativity
Operator precedence decides the grouping of terms in an expression. This affects how an expression is evaluated.
Certain operators have a higher precedence than others; for example, the multiplication operator has a higher precedence than the addition operator -
For example x = 10+3*2; Here, operand 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 show at the top of the table, and those with the lowest appear at the bottom. Within an expression, the higher precedence operator will be evaluated first.
Certain operators have a higher precedence than others; for example, the multiplication operator has a higher precedence than the addition operator -
For example x = 10+3*2; Here, operand 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 show at the top of the table, and those with the lowest appear at the bottom. Within an expression, the higher precedence operator 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 |
Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial guide, we are going to be discussing about the Arithmetic Operators.
Feel free to ask your questions where necessary and we 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 we 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.