Operators in PHP

Operators in PHP

PHP language supports the following types of operators.

  • Arithmetic Operators
  • Comparison Operators
  • Logical (or Relational) Operators
  • Assignment Operators
  • Conditional (or ternary) Operators

5.1. Arithmetic Operators

These operators perform necessary arithmetic operations on the operands. These computations include all the operations that are defined in the table below.

Let x and y be two operands

Operator

Operation Expression Description
+ Addition x+y Returns sum of a&b
- Subtraction x-y

Returns difference of a&b

* Multiplication x*y

Returns product of a&b

/ Division x/y

Returns quotient of a&b

% Modulus x%y

Returns remainder of a&b

++ Increment x++

Increments a by 1

== Decrement x--

Decrements by 1

5.2. Assignment Operators

There are following assignment operators supported by PHP language

Operator Description
= Assigns values from right side operands to left side operand
+= It adds right operand to the left operand and assign the result to left operand
-= It subtracts right operand from the left operand and assign the result to left operand
*= It multiplies right operand with the left operand and assign the result to left operand
/= It divides left operand with the right operand and assign the result to left operand
%= It takes modulus using two operands and assign the result to left operand

5.3. Comparison Operators

Comparison operators unlike arithmetic operators only compare two values and return true or false as the result.

Operator Operation Expression x=y x>y x<y
== Equal to x==y True False False
!= Not Equal to x!=y False True True
<= Less Than equal to x<=y True False True
>= Greater than equal to x>=y True True False
< Less than x<y False False True
> Greater than x>y False True False

5.4. Conditional Operator

There is one more operator called the 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.

Syntax:

If Condition is true ? Then value X : Otherwise value Y

5.5. Logical Operators

These are following logical operators supported by PHP language

Operator Description
and If both the operands are true then condition becomes true
or If any of the two operands are non zero then condition becomes true
&& If both the operands are non zero then condition becomes true
| | If any of the two operands are non zero then condition becomes true
! If a condition is true then Logical NOT operator will make false