Operator is a mathematical symbol which used to tell compiler to do mathematical operations and logical operations. Operators operates on variables and constants. These variables or constants are called operands. Operators and Operator form an expression.
Following are type of operators in PHP.
Increment & Decrement operators:-
These operators are used for increasing and decreasing values of variable.These operators need only one operand so they operators are also called Unary operators.
Some examples of unary operators.
a++,++a,--a,a--
<?php
$a = 10;
echo $a++;
echo "<br/>";
echo ++$a;
echo "<br/>";
echo --$a;
echo "<br/>";
echo $a--;
?>
Arithmetic Operator:-
These operators are used for addition, substration , multiplication and divisions operations.These operators use two operands there fore These operators are also called binary operators.
Some examples of Binary Operators.
+,-,*,/,%
<?php
$a = 10;
$b = 3;
$c = $a + $b;
echo $c;
echo "<br/>";
$c = $a - $b;
echo $c;
echo "<br/>";
$c = $a * $b;
echo $c;
echo "<br/>";
$c = $a / $b;
echo $c;
$c = $a % $b;
echo $c;
?>
Assignment Operators:-
These operatos are used for assign values to variables.
' = ' is an example of assignment operator.
<?php
$a = 20; // Assignment Operator assign 20 to variable a
echo $a ;
?>
Arthmetic Assignment Operators:-
These operators are used for both arthmetic and assignment operation.
Below are examples of Arthmatic Assignment operators.
+=,-=,*=,/=
<?php
$a = 10;
$b = 3;
$b +=$a;
echo $b;
echo "<br/>";
$b -=$a;
echo $b;
echo "<br/>";
$b *=$a;
echo $b;
echo "<br/>";
$b /=$a;
echo $b;
echo "<br/>";
?>
Comparison operators:-
These operators are used for comparing variable values.
= =,<,>,!= are examples of comparison operators.
<?php
$a = 20;
$b =20;
if($a == $b){
echo "Equal";
}
$a = 20;
$b = 2;
echo "<br/>";
if($a != $b){
echo "Not Equal";
}
echo "<br/>";
if($a > $b){
echo "a greater than b";
}
echo "<br/>";
$a = 2;
$b = 20;
if($a < $b){
echo "a less than b";
}
echo "<br/>";
if($a != $b){
echo "a not equal to b";
}
?>
Logical Operators:-
These operators are used for logical operations like true or false.
&& , || , ! are logical operators.
<?php
$a = 20;
$b =20;
$c = 20;
if($a == $b && $a == $c){
echo "true";
}
echo "<br/>";
if($a == $b || $a == $c){
echo "true";
}
echo "<br/>";
$a =2;
$b = 3;
if($a != $b){
echo "true";
}
?>
Following are type of operators in PHP.
Increment & Decrement operators:-
These operators are used for increasing and decreasing values of variable.These operators need only one operand so they operators are also called Unary operators.
Some examples of unary operators.
a++,++a,--a,a--
<?php
$a = 10;
echo $a++;
echo "<br/>";
echo ++$a;
echo "<br/>";
echo --$a;
echo "<br/>";
echo $a--;
?>
Arithmetic Operator:-
These operators are used for addition, substration , multiplication and divisions operations.These operators use two operands there fore These operators are also called binary operators.
Some examples of Binary Operators.
+,-,*,/,%
<?php
$a = 10;
$b = 3;
$c = $a + $b;
echo $c;
echo "<br/>";
$c = $a - $b;
echo $c;
echo "<br/>";
$c = $a * $b;
echo $c;
echo "<br/>";
$c = $a / $b;
echo $c;
$c = $a % $b;
echo $c;
?>
Assignment Operators:-
These operatos are used for assign values to variables.
' = ' is an example of assignment operator.
<?php
$a = 20; // Assignment Operator assign 20 to variable a
echo $a ;
?>
Arthmetic Assignment Operators:-
These operators are used for both arthmetic and assignment operation.
Below are examples of Arthmatic Assignment operators.
+=,-=,*=,/=
<?php
$a = 10;
$b = 3;
$b +=$a;
echo $b;
echo "<br/>";
$b -=$a;
echo $b;
echo "<br/>";
$b *=$a;
echo $b;
echo "<br/>";
$b /=$a;
echo $b;
echo "<br/>";
?>
Comparison operators:-
These operators are used for comparing variable values.
= =,<,>,!= are examples of comparison operators.
<?php
$a = 20;
$b =20;
if($a == $b){
echo "Equal";
}
$a = 20;
$b = 2;
echo "<br/>";
if($a != $b){
echo "Not Equal";
}
echo "<br/>";
if($a > $b){
echo "a greater than b";
}
echo "<br/>";
$a = 2;
$b = 20;
if($a < $b){
echo "a less than b";
}
echo "<br/>";
if($a != $b){
echo "a not equal to b";
}
?>
Logical Operators:-
These operators are used for logical operations like true or false.
&& , || , ! are logical operators.
<?php
$a = 20;
$b =20;
$c = 20;
if($a == $b && $a == $c){
echo "true";
}
echo "<br/>";
if($a == $b || $a == $c){
echo "true";
}
echo "<br/>";
$a =2;
$b = 3;
if($a != $b){
echo "true";
}
?>

No comments:
Post a Comment