Logical Operator in Php


An operator is a symbol that represent a specific action.Operators are used to perform operations on variables and values.

Logical Operators
There are three types of logical operators
a) Logical AND operator
b) Logical OR operator
c) Logical NOT operator

a) Logical AND operator
In logical AND operator we check more than one conditional and all the condition must get true if we want to execute the statement. The best example of logical AND operator login system. In php double am-percent ( && ) sign is used as logical AND operator.
Example
<!DOCTYPE html>
<html>
<head><title>expertcodingmaster.com</title>
</head>
<body>
<?php
$username = 'expertcodingmaster';
$password = 123456;
if($username =='expertcodingmaster' && $password ==123456)
{
echo "welcome to expertcodingmaster.com";
}
else
{
echo "check your username and password";
}
?>
</body>
</html>

Output of the above code
welcome to expertcodingmaster.com


b) Logical OR operator
In logical OR operator if any condition get true then it will execute the statement. In Php double pipe ( || ) sign is used as logical OR operator.
Example
<!DOCTYPE html>
<html>
<head><title>expertcodingmaster.com</title>
</head>
<body>
<?php
$username = 'expertcodingmaster';
$password = 123456;
if($username =='expertcodingmaster' || $password ==1)
{
echo "welcome to expertcodingmaster.com";
}
else
{
echo "check your username and password";
}
?>
</body>
</html>
Unlike AND operator we don't need to get both the condition true. If any one of these get true it will execute the if block. Else block are only executed if both the condition are get false.


The output of the above code
welcome to expertcodingmaster.com

c) Logical NOT operator
In Php exclamation mark ( ) is used as logical OR operator.
Example
<!DOCTYPE html>
<html>
<head><title>expertcodingmaster.com</title>
</head>
<body>
<?php
$a = "Hello world";
$b = "";
if(!empty($a))
{
echo "The variable A has some value";
}
else
{
echo "The variable A no value";
}
if(!empty($b))
{
echo "The variable B has some value";
}
else
{
echo "The variable B has no value";
}
?>
</body>
</html>

The output of the above code
The variable A has some value
The variable B has no value 

No comments: