PHP logical operators
Logical operators really come into their own when combined with conditional tests.following example illustrating these 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). |
Output
This program will redirect you on “http://www.phptpoint.com” page
In the above exampleThis program will redirect you on “http://www.phptpoint.com” page
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 “http://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). |
Output
This program will redirect you on “http://www.phptpoint.com” page
in the above exampleThis program will redirect you on “http://www.phptpoint.com” page
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 “http://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 “http://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. |
Output
11 is odd number
In the above example11 is odd number
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
Output
wrong email or pass
Enter your email
Enter your pass
wrong email or pass
Enter your email
Enter your pass
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 comments:
Post a Comment