Instructions for using php && logical AND operator_PHP tutorial

WBOY
Release: 2016-07-21 15:40:31
Original
935 people have browsed it

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

o(︶︿︶)o Oh, I’m so confused. I asked so many people today. I finally figured out what’s going on with the “&&” thing.

operators don’t even judge what it means to write like that. Alas, it turns out that if the previous one is false. The subsequent statements will not be executed. It saves us the trouble of writing if

. It’s so simple. . .

//Simple explanation, if the previous judgment is false, the following one will not be executed. If it is true, continue to execute the following definition of constant operation.

逻辑运算符
例子 名称 结果
$a and $b And(逻辑与) TRUE,如果 $a 与 $b 都为 TRUE
$a or $b Or(逻辑或) TRUE,如果 $a 或 $b 任一为 TRUE
$a xor $b Xor(逻辑异或) TRUE,如果 $a 或 $b 任一为 TRUE,但不同时是。
! $a Not(逻辑非) TRUE,如果 $a 不为 TRUE
$a && $b And(逻辑与) TRUE,如果 $a 与 $b 都为 TRUE
$a || $b Or(逻辑或) TRUE,如果 $a 或 $b 任一为 TRUE

Example #1 Logical operator example
Copy code The code is as follows:

// The following foo() will not be called because they are "short-circuited" 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 the value (false || true), and the result is true
$f = false or true; // $f is assigned false [Altair note: "=" has a higher priority than "or"]
var_dump($e, $f);
// "&&" has a higher priority than "and"
$g = true && false; // $g is assigned the value (true && false), and the result is false
$h = true and false; // $h is assigned true [Altair note: "=" has a higher priority than "and"]
var_dump($g, $h);
?>

The output of the above example is similar to:
bool(true)
bool(false)
bool(false)
bool(true)

Another example that might help.

(isset($panelemail) && !empty($panelemail) ? $panelemail : $userdata['email']);
?>
returns the userdata email address, but this

(isset($panelemail) AND !empty($panelemail) ? $panelemail : $userdata['email']);
?>
returns false.

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

For example:

true && false || false
?>
returns false, but

true AND false || false
? >
returns true.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321384.htmlTechArticleExample: !defined('MAGIC_QUOTES_GPC') define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc()); o(︶︿ ︶)o Oh, I’m so confused. I asked so many people today. Finally figured out how to "" things...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template