Php Operators :
There are many types of operators in php.
- Arithmetic Operators
- Comparison Operators
- Assignment Operators
- Increment/Decrement Operators
- Logical Operators
- String operators
- Error Control Operators
- Array Operators
- Bitwise Operators
- Type Operators
- Execution Operators
- Error Control Operators
1 Arithmetic Operators
+ (addition)
- ( subtraction) This operation work as a subtraction as well as negation
$a - $b ( this is the example of subtraction)
-$a ( this is the example of negation that means opposite of $a )
* (multiplication)
% ( modulus
/ ( division)
** ( exponentiation ) ( This will work in PHP 5.6 version or later)
Example of arithmetic operators :
<?php
$var1 = 100;
$var2 = 200;
$sum = $var1 + $var2;
$subtraction = $var2 - $var1;
$multiplication = $var1 * $var2;
$division = $var2/$var1;
$modulus = $var2%$var1;
echo "Sum = $sum<br>";
echo "Subtraction = $subtraction<br>";
echo "Multiplication = $multiplication<br>";
echo "Division = $division<br>";
echo "modulus = $modulus<br>";
?>
Output :
Sum = 300
Subtraction = 100
Multiplication = 20000
Division = 2
modulus = 0
2 Comparison Operators
== ( Equal )
=== ( Identical )
!= (Not Equal )
!== ( Not Identical )
<> ( Not Equal )
>= ( Greater than or equal to )
<= ( Less than or equal to )
> ( Greater than )
< ( Less than )
Simple example of the above mentioned operators.
<?php
$var1 = 100;
$var2 = 100.00;
$var3 = 100.00;
$var4 = 50;
if($var1==$var2){
echo "Equal comparison operator<br>";
}
/**
* This condition work if and only if
* both operands are same in value as well as data type
* In this case $var1 and $var2 are same in value but not in data type.
* So this condition will not work.
*/
if($var1===$var2){
echo "Identical comparison operator<br>";
}
/**
* This condition will work because $var2 and $var3 are same in value as well as data type
*/
if($var2===$var3){
echo "Identical comparison operator<br>";
}
if($var1 > 50){
echo "Greater than operator<br>";
}
if($var1 < 50){
echo "Less than operator<br>";
}
if($var1 >= 100){
echo "Greater than or equal operator<br>";
}
if($var1 <= 100){
echo "Less than or equal operator<br>";
}
if($var1 != $var4){
echo "Not equal operator<br>";
}
if($var1 <> $var4){
echo "Not equal operator<br>";
}
if($var1 !== $var2){
echo "Not identical operator<br>";
}
?>
Output :
Equal comparison operator
Identical comparison operator
Greater than operator
Greater than or equal operator
Less than or equal operator
Not equal operator
Not equal operator
Not identical operator
3 Assignment Operators
= ( This is assignment operator )
<?php
$a =100;
echo "Value of a = $a<br>";
$a+=100; // $a = $a + 100;
echo "Value of a = $a<br>";
$a-=100; //$a = $a-100
echo "Value of a = $a<br>";
$a*=100; // $a = $a*100
echo "Value of a = $a<br>";
?>
Output :
Value of a = 100
Value of a = 200
Value of a = 100
Value of a = 10000
4 Increment/Decrement Operators
++ ( Increment Operator )
- - ( Decrement Operator )
$a++ // $a++ and $a=$a+1 are same
$a-- // $a-- and $a=$a-1 are same
Understand with simple example.
1 )
<?php
$a = 100;
echo "Value of a = $a<br>";
/**
* Now you can increase the value of $a by 1
* using $a++( $a++ means $a=$a+1)
*/
$a++;
echo "After increment the value of a = $a<br>";
/**
* Now you can decrease the value of $a by 1
* using $a-- ( $a-- means $a=$a-1
*/
$a--;
echo "After decrement the value of a = $a<br>";
?>
Output :
Value of a = 100
After increment the value of a = 101
After decrement the value of a = 100
5 Logical Operators
There are three logical operators:
1) && ( And operator works on two operands like $operand1 && $operand2)
2) || ( Or operator works on two operands like $operand1 || $operand2)
3) ! ( Not operator works on one operands like !$operand1)
Lets take a simple example to understand these all three operators.
You can copy past to check the blow code as well.
<?php
/**
*
* In this block we will try to understand && logical operator
*/
$operand1=true;
$operand2=true;
if($operand1 && $operand2){ // If both are true then its work
echo "Condition is true<br>";
}
$operand1=false;
$operand2=true;
if($operand1 && $operand2){ // If both are true then its work
echo "If condition is true because of both operands are true<br>";
}else{ // If any of the operands or both operands are false then this block will work
echo "Else block will execute because of any of the operands or both operands are false<br>";
}
/**
* Output of the above block is below:
* Condition is true
* Else block will execute because of any of the operands or both operands are false
*/
/**
* In this block we will try to understand || logical operator
*/
$operand1=false;
$operand2=true;
if($operand1 || $operand2){ // If any one operand is true then this block will work
echo "Condition is true<br>";
}
$operand1=false;
$operand2=false;
if($operand1 || $operand2){ // If any one operand is true then this block will work
echo 'If any one operand is true then this block will work<br>';
}else{ // If both operands are false then this block will work
echo "Else block will execute because of both operands are false<br>";
}
/**
* Output of the above block is below:
* Condition is true
* Else block will execute because of both operands are false
*/
/**
* Lets take a example to understand ! (not) operator
*/
$operand1=false;
if(!$operand1){ // This means if not true then this block will work
echo " This means if not true then this block will work<br>";
}
$operand1 =10;
if($operand1==10){
echo "Operand1 value is 10<br>";
}
$operand1 =11;
if($operand1!=10){
echo "Operand1 value is not 10<br>";
}
/**
* Output of the above block is below:
* This means if not true then this block will work
* Operand1 value is 10Operand1 value is not 10
*/
First time visiting your website, I like your web site!