Home >>PHP Tutorial >PHP Logical Operators

PHP Logical Operators

PHP logical operators

Logical operators really come into their own when combined with conditional tests. following example illustrating these operators.

Logical Operators
Operatos Description
&& and
|| or
! not

AND(&&) Operator

Operator  name and pass
Description  If name and pass both are true then result true.
Explanation  if name = = "alex" and pass = = "alex123" then it will redirect on phptpoint page, and if any one of these is not valid then it shows an error message(Invalid name or password).

Eg of and operator

<?php
		
$name="alex";
		
$pass="alex123";
		
if($name=="alex" && $pass=="alex123")
		
{
			
header('location:https://www.phptpoint.com');
		
}
		
else
		
{
		
echo "Invalid name or password";	
		
}
				
?>

Output This program will redirect you on "https://www.phptpoint.com" page

In the above example Two variable $name and $pass with value("alex","alex123") If both value are exist then it will redirect on phptpoint.com because of header( ). Otherwise invalid name or password. Here both the condition are true so as output you will be redirected on "https://www.phptpoint.com" page.


OR(||) Operator

Operator  name or pass
Description  If name or pass are true then result true.
Explanation  if name = = "alex" or pass = = "alex123" then it will redirect on phptpoint page, and if both are false then it shows an error message(Invalid name or password).

Eg

<?php
		
$name="alex";
		
$pass="alex123";
		
if($name=="alex" || $pass=="alex12345")
		
{
			
header('location:https://www.phptpoint.com');
		
}
		
else
		
{
		
echo "Invalid name or password";	
		
}
				
?>

Output This program will redirect you on "https://www.phptpoint.com" page

in the above example Two variable $name or $pass $name hold the value="alex" $pass hold the value="alex123" if any of one condition is true then redirect you on "https://www.phptpoint.com" page otherwise so invalid name or password. Here one of the both condition is true so as output you will be redirected on "https://www.phptpoint.com" page.


Not(!) Operator

Operator  not
Description  reverse the logical test
Explanation  check given number is odd or not. Here $num stores 11 and its modulus is 1. By example $num mudulus is not equal to 0 it is an odd number so 11 is an odd numder.

Eg

<?php
		
$num=11;
		
if($num%2!=0)
		
{
			
echo $num." is odd number";
		
}
		
else
		
{
			
echo $num." is even number";	
		
}
				
?>

Output 11 is odd number

In the above example take a variable $num with value = 11 let we check number is odd or even. we give a condition($num%2!=0) inside if it will not true then number is odd. otherwise else statement is execute ( Number is even ). Here number is not divided by 2 so the output display : given number is odd number

Create a Login page using && and || operator

<?php
     
if(isset($_GET['login']))
       
{
        
$eid=$_GET['e'];
        
$pass=$_GET['p'];
	
if($eid=="" || $pass=="")
	
{
	
echo "<font color='red'>Please fill your email and pass</font>";
	
}
	
else
	
{
		
if($eid=="xyz" && $pass=="xyz123")
		
{
               
 echo "<font color='blue'>welcome xyz</font>";
		
}
		
else
		
{
		
echo "<font color='red'>wrong email or pass</font>";
		
}
	
}
	
}
				
?>

<form>
	
Enter your email<input type="text" name="e"/><br/>
	
Enter your pass<input type="password" name="p"/>
	
<input type="submit" value="Signin" name="login"/>

</form>

Output
wrong email or pass
Enter your email
Enter your pass

In the above example Here we create a form of two field .By default the method of form is 'GET' First filed is for Email Second filed is for password A logic is define in PHP script. First it's check the isset( ) function for existence, Enter the name and password in name and password field it store value in variables ($eid and $pass). if either $eid or $pass value is null then a message is shown "fill your email or password". Otherwise it check the $eid and $Pass value with the given existing value. if match then message show "welcome xyz" else message show "wrong email or password."


No Sidebar ads