PHP && Logic and operator usage Instructions _php tips

Source: Internet
Author: User
Tags logical operators
Example:
!defined (' MAGIC_QUOTES_GPC ') && define (' MAGIC_QUOTES_GPC ', GET_MAGIC_QUOTES_GPC ());

O (︶^︶) o Alas, very dizzy, today asked n many people. I finally got the "&&" thing figured out what's going on.

Operators do not have to judge what it means to write, hey, if the front is false. The following statement is not executed. Lest we still bother to write if

How simple it is ...

A simple description, if the previous judgment is False then does not execute, if it is true, continue to perform the following definition of the constant operation.
logical Operators
Example name Results
$a and $b and (Logic and) trueif both the $a and the $b are true.
$a or $b or (logical OR) trueif either $a or $b is true.
$a XOR $b Xor (logical XOR OR) trueif either $a or $b is true, but not at the same time.
! $a Not (logical non) trueIf the $a is not true.
$a && $b and (Logic and) trueif both the $a and the $b are true.
$a | | $b or (logical OR) trueif either $a or $b is true.

Example #1 Logical Operators Example
Copy Code code as follows:

<?php
The following foo () is not invoked because they are "shorted out" 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 (false | | true), and the result is true
$f = False or true; The $f is assigned false [Altair NOTE: "=" has a higher precedence than "or"]
Var_dump ($e, $f);
"&&" has higher precedence than "and"
$g = True && false; The $g is assigned (true && false) and the result is false
$h = True and false; The $h is assigned a value of true [Altair Note: "=" has a higher precedence than "and"]
Var_dump ($g, $h);
?>

The output of the example above 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 this two types of ands have a different order of precedence. "&&" is higher than "and", and "?:" operator just happens to come between the two. Also, since "| |" (or) is actually higher than "and" for 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.

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.