PHP - Operator Types
PHP - Operator Types
PHP - Operator Types
What is Operator? Simple answer can be given using expression 4 + 5 is equal to 9. Here 4 and
5 are called operands and + is called operator. PHP language supports following type of
operators.
Arithmetic Operators
Comparison Operators
Logical (or Relational) Operators
Assignment Operators
Conditional (or ternary) Operators
Arithmetic Operators
Show Examples
Comparison Operators
Show Examples
Operator Description Example
Checks if the value of two operands are equal or not, if yes then condition (A == B) is not
==
becomes true. true.
Checks if the value of two operands are equal or not, if values are not equal
!= (A != B) is true.
then condition becomes true.
Checks if the value of left operand is greater than the value of right operand, if (A > B) is not
>
yes then condition becomes true. true.
Checks if the value of left operand is less than the value of right operand, if yes
< (A < B) is true.
then condition becomes true.
Checks if the value of left operand is greater than or equal to the value of right (A >= B) is not
>=
operand, if yes then condition becomes true. true.
Checks if the value of left operand is less than or equal to the value of right
<= (A <= B) is true.
operand, if yes then condition becomes true.
Logical Operators
Show Examples
Called Logical AND operator. If both the operands are true then condition (A and B) is
and
becomes true. true.
Called Logical OR Operator. If any of the two operands are non zero then (A or B) is
or
condition becomes true. true.
Called Logical AND operator. If both the operands are non zero then condition (A && B) is
&&
becomes true. true.
Called Logical OR Operator. If any of the two operands are non zero then (A || B) is
||
condition becomes true. true.
Called Logical NOT Operator. Use to reverses the logical state of its operand. If a !(A && B) is
!
condition is true then Logical NOT operator will make false. false.
Assignment Operators
Show Examples
Simple assignment operator, Assigns values from right side C = A + B will assign value
=
operands to left side operand of A + B into C
Add AND assignment operator, It adds right operand to the left C += A is equivalent to C =
+=
operand and assign the result to left operand C+A
Divide AND assignment operator, It divides left operand with the C /= A is equivalent to C =
/=
right operand and assign the result to left operand C/A
Conditional Operator
There is one more operator called conditional operator. This first evaluates an expression for a
true or false value and then execute one of the two given statements depending upon the result of
the evaluation. The conditional operator has this syntax −
Show Examples
Operators Categories
All the operators we have discussed above can be categorised into following categories −
The conditional operator (a ternary operator), which takes three operands and evaluates
either the second or third expression, depending on the evaluation of the first expression.
Operator precedence determines the grouping of terms in an expression. This affects how an
expression is evaluated. Certain operators have higher precedence than others; for example, the
multiplication operator has higher precedence than the addition operator −
For example x = 7 + 3 * 2; Here x is assigned 13, not 20 because operator * has higher
precedence than + so it first get multiplied with 3*2 and then adds into 7.
Here operators with the highest precedence appear at the top of the table, those with the lowest
appear at the bottom. Within an expression, higher precedence operators will be evaluated first.