search

先看一段代码

$a = 5; $b = 20;if ($a = 10 || $b = 7) {    $a++;    $b++;}echo $a,"\n","",$b; //1 21

刚开始接触可能一眼看过去很容易说出最后的输出是5 20 但是仔细看后()里的条件表达式是赋值操作,所以应该输出11 21.但是计算机和我们开了一个并不好笑的玩笑。她妹的,最后竟然输出的是1 21。

计算机的思路和人脑的思路是有区别的,在编程的时候必须了解计算机的思维模式:机械式的思维模式。我们人在判断这个逻辑表达式的时候会跳跃性的先对$a和$b赋值,然后再做与运算,这样做虽然不影响逻辑运算的结果,但是也会产生上面这种副作用。事实上,计算机为了节省资源,采取了短路运算的方式解释这个逻辑表达式。当他发现与运算符的前一个数值(例如$a)是false时,他就会判断这个与运算表达式的运算结果是false,然后跳出这个逻辑运算表达式,继续执行下面的程序,而不会执行表达式剩下的语句。

至于这里为什么$a的值是1? 是因为在条件表达式的时候$a = 10的操作的时候php会把$a转换成boolean。上面的例子中$a = 10即转换成了true.所以在{}中的$a++ 是不能进行加法运算的。但是如果花括号中的$a++换成$a = $a + 1 那么此时就可以进行数值运算了。

短路运算的精髓就是
逻辑与(&&)->检查运算符第一个表达式是否为false,如果false直接返回false,后续的表达式直接忽略。
逻辑或(||)->检查运算符第一个表达式是否为true,如果是true直接返回true,后续表达式直接忽略。
PS:&&的优先级高于and、||的优先级高于or、等号(=)的优先级高于and和or

补充
php<?php// 下面的 foo() 不会被调用,因为它们被运算符“短路”了。$a = (false && foo());$b = (true  || foo());$c = (false and foo());$d = (true  or  foo());// "||" 的优先级比 "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);?>//以上例程的输出类似于:bool(true)bool(false)bool(false)bool(true)
Statement
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
Working with Flash Session Data in LaravelWorking with Flash Session Data in LaravelMar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

cURL in PHP: How to Use the PHP cURL Extension in REST APIscURL in PHP: How to Use the PHP cURL Extension in REST APIsMar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Simplified HTTP Response Mocking in Laravel TestsSimplified HTTP Response Mocking in Laravel TestsMar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

12 Best PHP Chat Scripts on CodeCanyon12 Best PHP Chat Scripts on CodeCanyonMar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

How to Register and Use Laravel Service ProvidersHow to Register and Use Laravel Service ProvidersMar 07, 2025 am 01:18 AM

Laravel's service container and service providers are fundamental to its architecture. This article explores service containers, details service provider creation, registration, and demonstrates practical usage with examples. We'll begin with an ove

Explain the concept of late static binding in PHP.Explain the concept of late static binding in PHP.Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

PHP Logging: Best Practices for PHP Log AnalysisPHP Logging: Best Practices for PHP Log AnalysisMar 10, 2025 pm 02:32 PM

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

Customizing/Extending Frameworks: How to add custom functionality.Customizing/Extending Frameworks: How to add custom functionality.Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version