OPERATORS IN PHP
An operator is a symbol that takes one or more values called operands and operates on them to
produce a result. A combination of an operator and operands is called an expression.
Types of Operators
1. Unary Operator
It is an operator that requires a single operand.
Example: Increment operator, Decrement operator, Negation Operators, Typecast
operators
2. Binary operator
It is an operator that requires two operands.
Example: Arithmetic operators, Relational operators, Logical operators, Assignment
operators, Bitwise operators, Concatenation operator, Reference Operator
3. Ternary operator / Conditional Operator
It is an operator that requires three operands.
Example: ?: operator
Types of Operators
1. Arithmetic Operators
Arithmetic operators are used to perform arithmetic operations.
OPERATOR DESCRIPTION
+ Addition
- Subtraction or Unary Negation
* Multiplication
/ Division
% Modulus. Gives remainder of division.
EXAMPLE:
<?php
$num1 = 40;
$num2 = 2.0;
$num7 = $num1 + $num2 * $num1 - $num2 / $num1;print "num7 = " . $num7.
"<BR>";
?>
2. Increment and Decrement Operators
The increment operator ++ adds one to the operand and the decrement operator -- subtracts
one from the operand.
OPERATOR DESCRIPTION
++ Adds 1 to its operand
-- Subtracts 1 from its operand
1
EXAMPLE:
<?php
$val = 5;
$val1 = ++$val;
$val2 = $val++;
print "val1 = " . $val1. "<BR>";print "val2 = " . $val2. "<BR>";
$val3 = --$val;
$val4 = $val--;
print "val3 = " . $val3. "<BR>";print "val4 = " . $val4. "<BR>";
?>
3. Relational Operators / Comparison Operators
Comparison operators are used to compare two quantities. The result of comparison can be
true or false.
OPERATOR DESCRIPTION
< Less than
<= Less than or Equal to
> Greater than
>= Greater than or Equal to
== Equal to
!= Not Equal to
=== Is identical to
!== Is not identical to
EXAMPLE:
<?php
$num1 = 40;
$num5 = '80'; if($num1 == $num5)
{
echo $num1 . " is equal to " . $num5 . "<BR>";
}
if($num1 === $num5)
2
{
echo $num1 . " is identical to " . $num5 . "<BR>";
}
if($num1 !== $num5)
{
echo $num1 . " is not identical to " . $num5 . " ;
}
?>
4. Logical Operators
Logical operators are used to combine two or more conditions and make decisions. It returns
either true or false.
OPERATOR DESCRIPTION
&& Logical AND.
Returns True if both the conditions are True.Otherwise, returns
False.
|| Logical OR.
Returns True if any one of the conditions isTrue. Otherwise,
returns False.
! Logical NOT.
Returns True if the operand is False.Returns False if the operand
is True.
EXAMPLE:
<?php
$num1 = 40;
$num2 = 2.0;
$num3 = 20; if(($num1/$num2)&&($num1/$num3)||($num2*$num3==$num1) )
{
echo $num1 . " is divisible by " . $num2 . " and " .
$num3 . "<BR>";
}
?>
3
5. Assignment Operators
Assignment operators are used to assign the result of an expression to a variable.
OPERATOR DESCRIPTION
= Assigns the value of the
expression on the right of the
=operator to the variable on
its left.
Shorthand assignment operators
+=
-=
*=
/=
%=
EXAMPLE :
<?php
$num1 = 40;
$num2 = 2.0;
$num3 = $num1;
$num1 /= $num2;
print "num1 = " . $num1. "<BR>";print "num3 = " . $num3. "<BR>";
?>
6. Bitwise Operators
Bitwise operators perform an operation on the bitwise representation of their operands. The
operands are converted to integer (except string) and the operation is performed. If both
operands are strings, the operation is performed between character offsets of the two strings.
OPERATOR DESCRIPTION
& Bitwise AND.
Returns bit 1 if corresponding bits of the two are operands are 1.
Otherwise, it returns bit0.
4
| Bitwise OR.
Returns bit 0 if corresponding bits of the twoare operands are 0.
Otherwise, it returns bit
Bitwise XOR.
Returns 0 if corresponding bits of both operands have same value.
^
Otherwise, it returns 1.
7. Negation Operators
OPERATOR DESCRIPTION
! Logical negation. Returns True if the operand evaluates to False.
Returns False if the operand evaluates to True.
~ Bitwise Negation.
In case of a numeric operand, it returns the bitwise negation of its
bitwise representation.
In case of strings, a string of equal length, in which each character is
the bitwise negation of its corresponding character in the original
string.
EXAMPLE:
<?php
$num1 = 80;
$num2 = 102.52;
$num3 = (!$num1) + (~$num2);
echo "Sum of Logical number 80 and Bitwise number 102.52 =
"
. $num3;
?>
OUTPUT
Sum of Logical number 80 and Bitwise number 102.52 = - 103
5
8. Conditional Operator / Ternary operator
The conditional operator (?:) is used to make two-way decisions.
Syntax:
exp-1 ? exp-2 : exp-3;
where, exp-1, exp-2 and exp-3 are expressions.
exp-1 is evaluated first. If it is true, then exp-2 is evaluated. Otherwise, exp-3 is evaluated.
EXAMPLE:
<?php
$num = 40;
$msg = isset($num) ? "num is set" : "num is not set";print $msg;
?>
9. Reference Operator
Reference operator ‘&’ is used to create an alias to a variable. The original variable and
the reference variable will point to the same value. If the value of any one variable is changed,
the change is automatically reflected in the other variable.
EXAMPLE
<?php
$a = 400;
$b = &$a;
print "Value of a = " . $a . "<BR>";print "Value of b = " . $b .
"<BR>";
++$a;
print "Value of a = " . $a . "<BR>";print "Value of b = " . $b .
"<BR>";
++$b;
print "Value of a = " . $a . "<BR>";print "Value of b = " . $b .
"<BR>";
?>
6
10. String Concatenation Operator
String concatenation operator is used to combine two strings. PHP supports the string
concatenation operator ‘.’ (dot or period operator).
EXAMPLE:
print "num1 = " . $num1. "<BR>";
11. Typecast Operators
Typecast operators are used to perform explicit type conversion (type casting) of a value.
OPERATOR DESCRIPTION
int or integer Changes data type of a value to integer.
float, real, or double Changes data type of a value to floating point.
string Changes data type of a value to string.
bool or boolean Changes data type of a value to Boolean.
array Changes data type of a value to array.
object Changes data type of a value to object.
EXAMPLE
<?php
$num1 = 80;
$num2 = 102.52;
$val5 = (int) $num2;
$val6 = (float) $num1;
$val7 = (real) $num1;
$val8 = (double) $num2;
$val9 = (string) $num1;
$val10 = (boolean) $num1;
print "val5 = " . $val5. "<BR>";
print "val6 = " . $val6. "<BR>";
print "val7 = " . $val7. "<BR>";
print "val8 = " . $val8. "<BR>";
print "val9 = " . $val9. "<BR>";
print "val10 = " . $val10. "<BR>";
7