Friday, January 14, 2011

Logical Operator (PHP)

As well as the comparison operators we saw earlier, there's also something called Logical Operators,ah haa,dont be panic,ok?hehehe. It just typically use these when u want to test more than one condition at one time. For example, u could check to see whether the name and ID number or password are correct from the If Statement. Here's the list of Operands.

 Operand         Example                                     Meaning
   &&             $var1 && $var2            both of them must be true
    
     | |                $var1 | | $var2                At least one of them is true.
  
  AND            $var1 AND $var2         both of them must be true
  
  XOR            $var1 XOR $var2          At least one of them is true.,
                                                   but NOT both?(please read explanation below
  
   OR               $var1 OR $var2            At least one of them is true.
   
    !                      !$var1                           Is NOT something/same


 Here is the explanation:

The && Operator
The && symbols mean AND. Use this if u need both values must be true, as in our ID name and ID numbers test. After all, u don't want to let people in if they just get the ID name right but not the ID numbers! Here's an example:

$ID name ='user';
$
ID numbers ='password';

if ($ID name ='user' && $ID numbers ='password') 
{
print("Welcome !");
}
 

else {
print("Invalid
ID name or ID numbers or both of them!");
}


The if statement is just the same, but notice that two conditions are being tested:

                  $ID name ='user' && $ID numbers ='password

This says, "If   ID name is correct AND the ID numbers is ok, too, then let them in".


The | | Operator

It mean OR. This symbol needed when u only want one of ur conditions to be true. For example, suppose u want to grant a mystery prize to customer if they have spent more than RM50 OR they have the special key. Else they don't get any prize. u'd then code like this:

$total =50;
$special_key ='SK14525';

if ($total_spent =50 | | $special_key ='SK14525') 
{
print("U got the prize!");
}
 

else {
print("No prize for you!");
}


Only need ONE of them to be true. If either one of them is true, then the code gets executed. If they are both false, then PHP will move on.

AND and OR

These are the same as the first two! AND is the same as && and OR is the same as ||.
There is a subtle difference, but as a beginner, u can simply replace this:

$ID name ='user' && $ID numbers ='password

With this

$ID name ='user' AND $ID numbers ='password

And this:

$total=50 | | $special_key ='SK14525'

With this:

$total =50 OR $special_key ='SK14525'

but AND is a lot easier to read than &&. OR is a lot easier to read than ||.
The difference, incidentally, is to do with Operator Precedence.The full explanation is coming soon!

XOR

It's used when u want to test if one value of two is true but NOT both. If both values are the same, then PHP sees the expression as false . If they are both different, then the value is true. Suppose u had to pick a winner between two participants. Only one of them can win. It's an XOR situation!Here's the example:

$participant_one = true;
$
participant_two = true;
if ($participant_one XOR $participant_two) 
{
print("Only 1 winner!");
}
else {
print("Both can't win!");
}


running the script!

The ! Operator
 
Known as the NOT operator. u use it test whether something is NOT something else. Plus u can also use it to reverse the value of a true or false value. For example, u want to reset a variable to false, if it's been set to true, and vice versa. Here's some example to try:

$value = true;
if ($value = = true)
{
print(!$test_value);
}


The code above will print out the number 0! (we'll see why when we tackle Boolean values below.)
What we're saying here is, "If $value is true then set it to what it's NOT." What it's NOT is false, so it will now get this value. A bit confused? It's okey,soon u will understand it!


N/NOTE:the next part, we'll take a look at Boolean values.besides,please comment me!

No comments:

Post a Comment