Breaking News

PHP-MySQLi Series 5.5 || Logical Operator



Logical Operators:

Logical operators are used to test more than one condition and make decision.
In php mainly there are three types of logical operators. They are;
a) AND Operator (&&)
b) OR operator (||)
c) Not operator (!)


Operator Name Example result
&& And $x && $y True, if both $x and $y are true.
|| Or $x || $y True, if any one is true but not both
! Not $x True, if $x is not true.

Logical && operator
Logical && operators are used to check more than one condition. If all the conditions get true, than the statement will execute. Login system is the best example of Logical && operator.
Here, both the user name and user password must match to execute the statement.

Example:
<?php
$usrename='admin.;
$userpassword = 'password';
if($username == 'admin' && $userpassword == 'password'){
echo "Welcome";
}
else
{
echo "Please enter the valid e-mail id and password";
}
?>

Here in this example if the username and userpassword get matched. Welcome will display in the browser. if it fails to meet both the condition, it will display else statement that is Please enter the valid e-mail id and password.

Output:
Welcome

Logical || operator:
In logical Or operator only condition is necessary to be fulfilled/matched to execute the statement. If both conditions get true, it will not execute the statement.

Example:
<?php
$usrename='admin.;
$userpassword = 'password';
if($username == 'admin' && $userpassword == '12345'){
echo "Welcome";
}
else
{
echo "Please enter the valid e-mail id and password";
}
?>
Here in this example if the any username and userpassword get matched. Welcome will display in the browser. if it fails to meet both the condition, it will display else statement that is Please enter the valid e-mail id and password.

Output:
Welcome


Logical Not operator

<?php
$x = 100;
if ($x !==80){
echo "Hello World";
}
?>

Output:
Hello World

No comments