php logical operators



PHP Logical operators

Logical operators are used when we have conditional statement such as if statement.

PHP logical operators

&&      and

||         or

!          not

Here, we have two variables $uname and $upass. We have assign value to both variables “meera” for $uname and “ram” for $upass.

In below example if both values match then print “welcome to system” message if dont print “invalid username and password” message in example output.


Example of && logical operator

<?php

$uname=”meera”;
$upass=”ram”;

if($name==”meera” && $upass==”ram”)
{
echo “Welcome to System”;
}
else
{
echo “Invalid username or Password”;
}

?>

The output is:
Welcome to System


Logical || or operator

If we use && then both condition must be true but in || or operator one of condition true, it will display “welcome to system” message.

Example of || logical operator

<?php

$uname=”meera”;
$upass=”ram”;

if($name==”meera” || $upass==”meera123″)
{
echo “Welcome to System”;
}
else
{
echo “Invalid username or Password”;
}

?>

The output is:
Welcome to System

In above php example only one condition true, it will show output “welcome to system”.


 Example of ! (not) logical operator

<?php

$uname=”meera”;
$upass=”ram”;

if($name !=”meera1″ && $upass !=”meera123″)
{
echo “Welcome to System”;
}
else
{
echo “Invalid username or Password”;
}

?>

The output is:
Welcome to System

Leave a Reply

Your email address will not be published. Required fields are marked *