msgbartop
Adam Palmer MBCS CITP, Linux, PHP Programmer, MySQL Developer, Embedded Hardware, Security Consultant
Did my blog help you? Please link to me!
  dns test
 
RSS Feed
msgbarbottom

17 Dec 09 PHP Programmer – Logical Operators

PHP allows the use of boolean operators.

AND, OR, XOR and NOT. We can combine NOT with AND and OR to form the NAND and NOR operators respectively.

$a = ($b and $c); will return TRUE if both $b AND $c are TRUE, otherwise, it will return FALSE. This can also be specified as $a = ($b && $c)

$a = ($b or $c); will return TRUE if $b OR $c are TRUE, otherwise, it will return FALSE. This can also be specified as $a = ($b || $c);

$a = ($b xor $c); will return TRUE if $b OR $c are TRUE, but not if they are both TRUE, otherwise, it will return FALSE.

$a = (! $b); will return TRUE if $b is NOT TRUE.

$a = (!($b && $c)); will form NAND (NOT + AND)
$a = (!$b || $c)); will form NOR (NOT+AND);

What use is this to us? Here’s some [hopefully] self explainatory examples:

if ($user_option1 xor $user_option2)
{
//good, do some processing
} else {
echo “You need to select at least one but not both options”;
}

if ($logged_in and $verified_email)
{
//good, do some processing
} else {
echo “You need to log in AND verify your email address before continuing”;
}

if ( ($usertype == “paying”) or ($usertype = “expired”) )
{
//good, do some processing
} else {
echo “You need to be a paying or previously paying user to continue, not a free user.”;
}

Tags: , , , , , , , , , ,



Leave a Comment