An exception to PHP operator precedence
今天在老王的技术手册看到一个问题:
<?php if ($a = 100 && $b = 200) { var_dump($a, $b); }
输出是什么?
这个问题, 咋一看或许觉得简单, 但其实仔细推敲并不简单,
如果说布尔与之前的部分, 是由于优先级的问题, 但是如果仅仅是优先级的问题的话, 那么结果应该是:
$a = (100 && $b) = 200
而实际上的结果, 确实高优先级的&&让步给次优先级的=, 让 $b = 200 先结合了.
究其原因, 是因为PHP并不完全遵守优先级的定义, 这个在PHP的手册中也有说明:
Note: Although = has a lower precedence than most other operators, PHP will still allow expressions similar to the following: if (!$a = foo()), in which case the return value of foo() is put into $a.
这样的设计, 个人不发表看法, 反正在C语言中, 这样类似的语句是判定为语法错的. PHP采用这样的设计, 很可能是历史原因,
有好奇的同学, 会想知道到底为什么, 之前jayeeliu网友也问过:
laruence你好:
问一个php运算符优先级的问题
$t == 1 && $tt = 2
按照php运算符优先级应该是
(($t == 1) && $tt) = 2
这个顺序执行,但实际上应该是
($t == 1) && ($tt = 2)
我有些不太理解。
其实也简单, 运算符优先级是在存在二义性文法的时候的一种规约规则选择的手段, 而PHP的语法分析文件定义中, 却让等号和T_BOOLEAN_AND(&&)之前不存在了规约冲突:
expr_without_variable: // 有隐规则存在, 相当于T_BOOLEAN_AND成为了"一元操作符". | expr T_BOOLEAN_AND { zend_do_boolean_and_begin(&$1, &$2 TSRMLS_CC); } expr
最后, 顺便说一下, PHP对应于T_BOOLEAN_AND 还定义了 T_LOGICAL_AND(and) 和 T_LOGICAL_OR(or) , 这俩个的优先级都低于等号, 于是就会有了, 很多PHP入门教材示例代码中经典的:
$result = mysql_query(*) or die(mysql_error());
类似的还可以用or来实现三元操作符(?:)的功能:
$person = $who or $person = "laruence"; //等同于: $person = empty($who)? "laruence" : $who;
更多PHP相关知识,请访问PHP中文网!
The above is the detailed content of An exception to PHP operator precedence. 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



Windows does a great job of allocating system resources to the processes and programs that need it most by assigning priorities to them. Most applications you install will run perfectly fine at the default "normal" priority level. Sometimes, however, you may need to run a program, such as a game, at a higher level than the default normal level to improve its performance. But this comes at a cost, and it's a deal worth pondering. What happens when you set an app to high priority? Windows has a total of six priority levels for running different processes and programs - low, below normal, normal, above normal, high and real-time. Windows will rank and queue applications based on their priority. The higher the priority, the application

Notifications are a great tool for productivity, but they can sometimes be distracting. Whether you want to disable notifications entirely or for selected apps, this page is what you need. We'll also look at how to automatically disable and enable notifications using FocusAssist. Additionally, if the Settings app doesn't work for you, you can use tools like Command Prompt, Registry Editor, and Group Policy Editor for a geekier way to disable notifications. Check out the following tutorial to learn 7 ways to disable notifications on Windows 11. Why should you disable notifications on Windows 11? Disabling notifications has its various advantages, some of which are listed below. However, keep in mind that disabling notifications for important apps may

What is process priority? Computers are not that different from their creators. Although it may appear that they are multitasking, they are actually juggling between tasks spontaneously. But not all processes or programs are equally allocated resources. Important processes, such as those necessary to keep the system running as smoothly as possible, are given high priority, while those that only work peripherally can be assigned a lower priority. This helps the system run smoothly even when it is under a lot of stress. What is priority? Processes have 6 different priorities. These are as follows: Low – This is the lowest priority. A process with "low" priority will not receive the necessary resources until all other tasks are completed. BelowNorma

The new Task Manager in Windows 11 22H2 is a boon for power users. It now provides a better UI experience with additional data to keep tabs on your running processes, tasks, services, and hardware components. If you've been using the new Task Manager, you may have noticed the new productivity mode. what is it? Does it help improve the performance of Windows 11 systems? Let’s find out! What is Productivity Mode in Windows 11? Productivity mode is one of the tasks in Task Manager

In the C language, there is no root operator. The built-in function "sqrt()" is used to open the root, and the syntax "sqrt(value x)" is used; for example, "sqrt(4)" is to perform the square root operation on 4. , the result is 2. sqrt() is a built-in root operation function in C language. Its operation result is the arithmetic square root of the function variable; this function can neither operate negative values nor output imaginary results.

Customizing general notification settings Let’s start with the basics of notification settings. First, if you want to set up notifications on Windows 11, there are two ways to do it. The quickest way is to right-click the date and time portion in the corner of the taskbar and select Notification Settings. Alternatively, you can use the Start menu to open the Settings app and select Notifications in the System section (open by default). Here you'll see an overview of your notification settings. You can disable notifications entirely, or click on the first option, Notifications, to expand the drop-down menu. This menu has some additional options, such as turning off notification sounds. You can also choose whether you want notifications to appear on the lock screen, including specific settings for reminders and incoming calls.

For Golang developers, "invaliduseof...operator" is a common error. This error usually occurs when using variable-length parameter functions. It will be detected at compile time and indicate which parts have problems. This article will introduce how to solve this error. 1. What is a variable-length parameter function? A variable-length parameter function is also called a variable-parameter function. It is a function type in the Golang language. Using variable-length parameter functions, you can define multiple ones as follows

Detailed explanation of the Linux process priority adjustment method. In the Linux system, the priority of a process determines its execution order and resource allocation in the system. Reasonably adjusting the priority of the process can improve the performance and efficiency of the system. This article will introduce in detail how to adjust the priority of the process in Linux and provide specific code examples. 1. Overview of process priority In the Linux system, each process has a priority associated with it. The priority range is generally -20 to 19, where -20 represents the highest priority and 19 represents
