Operators are used to perform operations on some values. Means using an expression 4 + 5 is equal to 9. Here 4 and 5 are operands and + is called operator. Given below are the various groups of operators:
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Incrementing/Decrementing Operators
- Logical Operators
- String Operators
- Array Operators
Operator
|
Name
|
Syntax
|
Operation
|
+
|
Addition
|
$a + $b
|
Adds tow
operands
|
-
|
Subtraction
|
$a - $b
|
Subtract
second operand from the first
|
*
|
Multiplication
|
$a * $b
|
Multiply bot
operands
|
/
|
Division
|
$a / $b
|
Divide
numerator by de-numerator
|
%
|
Modulo
|
$a % $b
|
Reminder of
the operands
|
**
|
Exponentiation
|
$a ** $b
|
$a raised to
the power $y
|
Example 1 –
<?php
$a = 29; //Var 1
$b = 4; // var 2
echo ($a + $b), "\n"; //
Output 33
echo($a - $b), "\n"; //
Output 25
echo($a * $b), "\n"; //
Output 116
echo($a / $b), "\n"; //
Output 7.25
echo($a % $b), "\n"; //
Output 1
?>
2.
Assignment operator - The basic assignment operator is "=". These
operators are used to assign values to different variable, with or without
mid-operations.
Operator
|
Name
|
Syntax
|
Operation
|
=
|
Simple
assignment operator
|
$a = $b
|
Assigns
values from right side operands to left side operand
|
+=
|
Add and
assignment operator
|
$a += $b
|
Adds right
operand to the left operand and assign the result to left operand
|
-=
|
Subtract AND
assignment operator
|
$a -= $b
|
Subtracts
right operand from the left operand and assign the result to left operand
|
*=
|
Multiply AND
assignment operator
|
$a *= $b
|
Multiplies
right operand with the left operand and assign the result to left operand
|
/=
|
Divide AND
assignment operator
|
$a /= $b
|
Divides left
operand with the right operand and assign the result to left operand
|
%=
|
Modulus AND
assignment operator
|
$a %= $b
|
Takes
modulus using two operands and assign the result to left operand
|
<?php
// simple assign operator
$a = 75;
echo $a, "\n";
// add then assign operator
$a = 100;
$a += 200;
echo $a, "\n";
// subtract then assign operator
$a = 70;
$a -= 10;
echo $a, "\n";
// multiply then assign operator
$a = 30;
$a *= 20;
echo $a, "\n";
// Divide then assign(quotient)
operator
$a = 100;
$a /= 5;
echo $y, "\n";
// Divide then assign(remainder)
operator
$a = 50;
$a %= 5;
echo $a;
3. Comparison
operators - These
operators are used to compare two elements and outputs the result in Boolean
form.
Operator
|
Name
|
Syntax
|
Operation
|
==
|
Equal To
|
$a = = $b
|
Checks if
the value of two operands are equal then True otherwise False
|
!=
|
Not Equal To
|
$a != $b
|
If both the
operands are equal then return True otherwise False
|
<>
|
Not Equal To
|
$a <>
$b
|
If both the
operands are unequal then return True otherwise False
|
===
|
Identical
|
$a === $b
|
If both
operands are equal and the same type then return True otherwise False
|
!==
|
Not
Identical
|
$a !== $b
|
If both
operands are unequal and the different types then return True otherwise False
|
<
|
Less Than
|
$a < $b
|
If $a is
less than $b then return True otherwise False
|
>
|
Greater Than
|
$a > $b
|
If $a is
greater than $b then return True otherwise False
|
<=
|
Less Than or
Equal To
|
$a <= $b
|
If $a is
less than or equal to $b then return True otherwise False
|
>=
|
Greater Than
or Equal To
|
$a >= $b
|
If $a is
greater than or equal to $b then return True otherwise False
|
Example –
<?php
$x = 80;
$y = 50;
$z = "80";
/*Here used var_dump() function
explain in next tutorial */
var_dump($x == $z) +
"\n";
var_dump($x != $y) +
"\n";
var_dump($x <> $y) +
"\n";
var_dump($x === $z) +
"\n";
var_dump($x !== $z) +
"\n";
var_dump($x < $y) +
"\n";
var_dump($x > $y) +
"\n";
var_dump($x <= $y) +
"\n";
var_dump($x >= $y);
4. Increment/Decrement
operators - These
are called the unary operators as it work on single operands. These are used to
increment or decrement values. The PHP increment operators are used to
increment a variable's value. And the decrement operators are used to decrement
a variable's value.
Operator
|
Name
|
Syntax
|
Operation
|
++
|
Pre-Increment
|
++$a
|
Increments
$a by one, then return $a
|
--
|
Pre-Decrement
|
--$a
|
Decrements
$a by one, then return $a
|
++
|
Post-Increment
|
$a++
|
Return $a,
then Increments $a by one
|
--
|
Post-Decrement
|
$a--
|
Return $a,
then Decrements $a by one
|
Example –
<?php
$a = 2;
echo ++$a, " First increments
then prints \n";
echo $a, "\n";
$a = 2;
echo $a++, " First prints then
increments \n";
echo $a, "\n";
$a = 2;
echo --$a, " First decrements
then prints \n";
echo $a, "\n";
$a = 2;
echo $a--, " First prints then
decrements \n";
echo $a;
5.
Logical Operator - These are basically used to operate with
conditional statements and expressions. Conditional statements are based on
conditions. There are following logical operators supported by PHP
language.
Operator
|
Name
|
Syntax
|
Operation
|
and
|
Logical AND
|
$a and $b
|
If both
operands are true then True otherwise False
|
or
|
Logical OR
|
$a or $b
|
If either of
the operands is true then True otherwise False
|
xor
|
Logical XOR
|
$a xor $b
|
If either of
the operand is true then True and If both are true then False
|
&&
|
Logical AND
|
$a
&& $b
|
If both
operands are true then True otherwise False
|
||
|
Logical OR
|
$a || $b
|
If either of
the operands is true then True otherwise False
|
!
|
Logical NOT
|
!$a
|
If $a is
false then True
|
Example –
<?php
$a = 50;
$b = 30;
if ($a == 50 and $b == 30)
echo
"and Success \n";
if ($a == 50 or $b == 20)
echo
"or Success \n";
if ($a == 50 xor $b == 20)
echo
"xor Success \n";
if ($a == 50 && $b == 30)
echo
"&& Success \n";
if ($a == 50 || $b == 20)
echo
"|| Success \n";
if (!$c)
echo
"! Success \n";
6.
String Operators - In PHP two operator specially design for string.
Operator
|
Name
|
Syntax
|
Operation
|
.
|
Concatenation
|
$text1.$text2
|
Concatenation
of $text1 and $text2
|
.=
|
Concatenation
and assignment
|
$text1.=$text2
|
First concatenates
then assigns, same as $text1 = $text1.$text2
|
Example –
<?php
$a = "Hello";
$b = "Cyber";
$c = "Teak....";
echo $a . $b . $x, "\n";
$a .= $b . $c;
echo $a;
?>
7. Array
operators - These operators are used in case of arrays.
Operator
|
Name
|
Syntax
|
Operation
|
+
|
Union
|
$a + $b
|
Union of $a
+ $b
|
==
|
Equality
|
$a == $b
|
If $a and $b
have the same key / value pairs then return True
|
!=
|
Inequality
|
$a != $b
|
If $a is not
equal to $b then return True
|
===
|
Identity
|
$a === $b
|
If $x and $y
have the same key/value pairs in the same order and of the same types then
return True
|
!==
|
Non-identity
|
$a !== $b
|
If $a is not
identical to $y then return True
|
<>
|
Inequality
|
$a <>
$b
|
If both are
unequal then return True
|
Example –
<?php
$a = arrab("k" =>
"Car", "l" => "Bike");
$b = arrab("a" =>
"Train", "b" => "Plane");
var_dump($a + $b);
var_dump($a == $b) +
"\n";
var_dump($a != $b) +
"\n";
var_dump($a <> $b) +
"\n";
var_dump($a === $b) +
"\n";
var_dump($a !== $b) +
"\n";
0 Comments