


Differences in the use of PHP logical operators & and && as well as && and ||
Logical operators are nothing more than performing logical operations on values. (&&) or (||)" These two operators can speed up the running speed of PHP code in the program.
First look at a piece of code:
<?php $test="李四"; $test=="张三"&&$test="张三来了"; echo $test; //输出“李四” $test="李四"; $test=="张三"||$test="张三不在这里"; echo $test; //输出“张三不在这里” ?>
Why Will this result occur? If we follow the usual method, we must at least use an IF statement to determine. But now only two logical operations will change the value of the variable. Let's analyze its operation principle.
In both sides of the expression involved in the logical operation, operations are performed from left to right, and as long as one of the "AND" operations is false, the result of the entire expression will be. is false. Therefore, when the expression on the left is false, there is no need to perform the operation. This processing is undoubtedly beneficial to the running efficiency of the program. So as the title says, it is an efficient usage. Logical OR is different: as long as one is true, the entire expression is true. Therefore, if the left side is false, the expression on the right must be judged. If you understand or understand the above, you will not be aware of the result. It feels strange.
The above example can of course be realized through conditional judgment statements. The current situation is to reduce the amount of code and, most importantly, to increase the execution efficiency of the program. The key to grasping this is that the expression runs from left to right. It stops when the first value determines the value of the entire expression. It is worth mentioning that the right side can be an expression or an expression. Function, but it cannot be a series of statement combinations or output statements. After all, it is still a component of a logical expression.
For the "and" (&&) operation: x && y When x is false, directly. Skip, do not execute y;
For the "or" (||) operation: x||y When x is true, skip directly, do not execute y in & and && in php. Similarities and Differences
<?php $a=10; if($a>4 && (++$a>10)) { } //输出结果为11. echo $a; ?> <?php $a=10; if($a>4 and (++$a>10)) { } //输出结果为11. echo $a; ?> ************************************************************** <?php $a=10; if($a>4 && (++$a<10)) { } //输出结果为11. echo $a; ?> <?php $a=10; if($a>4 & (++$a<10)) { } //输出结果为11. echo $a; ?> ********************************************************* <?php $a=10; if($a<4 && (++$a>10)) { } //输出结果为10. echo $a; ?> <?php $a=10; if($a<4 & (++$a>10)) { } //输出结果为11. echo $a; ?> ******************************************************************* <?php $a=10; if($a<4 && (++$a<10)) { } //输出结果为10. echo $a; ?> <?php $a=10; if($a<4 & (++$a<10)) { } //输出结果为11. echo $a; ?> ******************************************************************* <?php // 下面的 sktest() 不被调用,原因是它们被运算符“短路”。 $a = (false && sktest()); $b = (true || sktest()); $c = (false and sktest()); $d = (true or sktest()); // "||" 的优先级比 "or" 高 $e = false || true; // $e 被赋值为 (false || true),结果为 true $f = false or true; // $f 被赋值为 false [Altair注:"=" 的优先级比 "or" 高] var_dump($e, $f); // "&&" 的优先级比 "and" 高 $g = true && false; // $g 被赋值为 (true && false),结果为 false $h = true and false; // $h 被赋值为 true [Altair注:"=" 的优先级比 "and" 高] var_dump($g, $h); ?>
The above is the detailed content of Differences in the use of PHP logical operators & and && as well as && and ||. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

There is no built-in sum function in C language, so it needs to be written by yourself. Sum can be achieved by traversing the array and accumulating elements: Loop version: Sum is calculated using for loop and array length. Pointer version: Use pointers to point to array elements, and efficient summing is achieved through self-increment pointers. Dynamically allocate array version: Dynamically allocate arrays and manage memory yourself, ensuring that allocated memory is freed to prevent memory leaks.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

In C language, the main difference between char and wchar_t is character encoding: char uses ASCII or extends ASCII, wchar_t uses Unicode; char takes up 1-2 bytes, wchar_t takes up 2-4 bytes; char is suitable for English text, wchar_t is suitable for multilingual text; char is widely supported, wchar_t depends on whether the compiler and operating system support Unicode; char is limited in character range, wchar_t has a larger character range, and special functions are used for arithmetic operations.

An application that converts XML directly to PDF cannot be found because they are two fundamentally different formats. XML is used to store data, while PDF is used to display documents. To complete the transformation, you can use programming languages and libraries such as Python and ReportLab to parse XML data and generate PDF documents.

PHP and Laravel are not directly comparable, because Laravel is a PHP-based framework. 1.PHP is suitable for small projects or rapid prototyping because it is simple and direct. 2. Laravel is suitable for large projects or efficient development because it provides rich functions and tools, but has a steep learning curve and may not be as good as pure PHP.
