Tuesday 29 November 2016

Chapter 3(PHP) Types of Operators in PHP with Program and Explanation

There are Basically 4 types of opertaors in php language .The four types of the operator that are used in php are

  1. Logical AND Operator
  2. Logical OR Operator
  3. Logical NOT Operator
  4. Logical XOROperator 

     1 Logical AND Operator

        
         Sign of And Operator is &&

         Logical AND Operator will return the result true if all the conditions are true  .

EXAMPLE

<?php

$auth = 1;
$status = 1;
$role = 4;


$result = (($auth == 1) && ($status == 1));
print "result is $result";

?>
We Can also use print in place of echo

EXPLANATION

Here we take three user define variables $auth,$status and $role. we can assign some values to these variables.
Now  we define the fouth variable $result in which we can use And operator like
(($auth == 1) && ($status == 1)); 
the sign == means comparing in this we are comparing that $auth is equal to 1 or not and $status is equal to 1 or not.But here both are equal so the both results are true and it will returns true .
As we know in operators
1 means true
0 means false
OUTPUT





  2 Logical OR Operator

    
     Sign of OR Operator is ||

     Logical OR Operator will return the result true if any condition is true  .

EXAMPLE

<?php

$auth = 1;
$status = 1;
$role = 4;


$result = (($auth == 1) || ($role <= 2));
print "result is $result";

?>

EXPLANATION

Here we take three user define variables $auth,$status and $role. we can assign some values to these variables.
Now  we define the fouth variable $result in which we can use Or operator like
(($auth == 1) || ($role <= 2)); 
the sign == means comparing in this we are comparing that $auth is equal to 1 or not and $role is less tan or equal to 2.But here first condition is true $auth=1 but $role is not less than or equal to 2 beacuse $auth is equal to 4.But in Or operator if any one condition is true than it will return result as true .

OUTPUT


 3 Logical NOT Operator

    
     Sign of NOT Operator is !

     Logical NOT Operator will return the result true if any condition is false  .

EXAMPLE

<?php

$status = 1;

$result = !($status == 1);
print "result is $result";

?>

EXPLANATION

Here we take  user define variables $status and assign a value to it.
Now  we define other variable $result in which we can use NOT operator like
$result = !($status == 1); 
the sign == means comparing in this we are comparing that $auth is equal to 1 or not .But here we place sign of NOT operator(!)  and it will become Not equal to one but at upper $status is equal to one.The condition is true so it will not return result as true it will not display anything on execution.

OUTPUT


  4 Logical XOR Operator

   

     Logical XOR Operator will return the result true if either of two conditions are true, or returns            false if both conditions are true

EXAMPLE

<?php

$auth = 1;
$status = 1;

$result = (($status == 1) xor ($auth==1));
print "result is $result";

?>

EXPLANATION

Here we take two user define variables $auth and $status . we can assign some values to these variables.
Now  we define the third variable $result in which we can use XOR operator like
 (($status == 1) xor ($auth==1))
the sign == means comparing in this we are comparing that $status is equal to one or not and $auth is also equal to one or not.But here both the conditions are true because $status=1 and Sauth=1 so it will not return result as true so it will not show anything on execution.

OUTPUT

**  INCREMENT AND DECREMENT Operator

Sign of increment (++)
Sign of decrement (--)

Increment operator is used to add one to the value that we assign to of user define variable.
Decrement operator is used to substract one from the value that we assign to of user define variable.

EXAMPLE(Increment)

<?php

$total = 10;
$total++;

echo $total;

?> 

EXPLANATION
     
        In this program we take a user define variable $total and assign a value 10 to it .here we have to increment it so we use increment opertaor like $total++; 
it means add one to the value of user define variable .The value of user define varibale is 10 and after increment it will become 11 and that we display in echo. 

OUTPUT


EXAMPLE(Decrement)

<?php

$total = 10;
$total--;

echo $total;

?> 

EXPLANATION
     
        In this program we take a user define variable $total and assign a value 10 to it .here we have to decrement it so we use decrement opertaor like $total--; 
it means substract one to the value of user define variable .The value of user define varibale is 10 and after decrement it will become 9 and that we display in echo. 

OUTPUT


If u have any query regarding this than post a comment. 


No comments:

Post a Comment