PHP && Logic and operator usage instructions

Source: Internet
Author: User

Example:
!defined (' MAGIC_QUOTES_GPC ') && define (' MAGIC_QUOTES_GPC ', GET_MAGIC_QUOTES_GPC ());

O (︶^︶) o Alas, very faint, today asked n many people. Finally get the "&&" thing figured out what's going on.

Operators are not judged to write what it means, eh, if the previous is false. The subsequent statement will not be executed. Lest we bother to write if

So much simpler ...

Simply stated, if the previous judgment is False then do not execute, if it is true, proceed to the following definition of the constant operation.

logical Operators
Example name Results
$a and $b and (Logical AND) trueif $a and $b are true.
$a or $b or (logical OR) trueif either $a or $b is true.
$a XOR $b Xor (Logical XOR) trueif either $a or $b is true, but not at the same time.
! $a Not (logical) trueIf the $a is not true.
$a && $b and (Logical AND) trueif $a and $b are true.
$a | | $b or (logical OR) trueif either $a or $b is true.


Example #1 Logical Operator Example

Copy CodeThe code is as follows:
<?php
The following foo () is not called because they are "shorted" by the operator.
$a = (false && foo ());
$b = (true | | foo ());
$c = (False and foo ());
$d = (true or foo ());
"| |" has a higher priority than "or"
$e = False | | True $e is assigned a value of (false | | true), and the result is true
$f = False or true; $f is assigned false [Altair NOTE: ' = ' has a higher precedence than ' or ']
Var_dump ($e, $f);
"&&" has a higher priority than "and"
$g = True && false; $g is assigned (true && false) and the result is false
$h = True and false; $h is assigned true [Altair Note: ' = ' has a higher precedence than ' and ']
Var_dump ($g, $h);
?>


The output of the previous example is similar to the following:
BOOL (TRUE)
BOOL (FALSE)
BOOL (FALSE)
BOOL (TRUE)

Another example that might help.

<?php
(Isset ($panelemail) &&!empty ($panelemail)? $panelemail: $userdata [' email ']);
?>
Returns the UserData email address, but this

<?php
(Isset ($panelemail) and!empty ($panelemail)? $panelemail: $userdata [' email ']);
?>
returns FALSE.

The reason is and the types of ands have a different order of precedence. "&&" is higher than "and", and the "?:" operator just happens to come between the other. Also, since "| |" (or) is actually higher than "and," You should never mix &&s and | | S with ANDs and ORs without paretheses.

For example:

<?php
True && false | | False
?>
returns false, but

<?php
True and False | | False
?>
Returns True.

PHP && Logic and operator usage instructions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.