Home Backend Development PHP Tutorial Detailed explanation of PHP operator priority order

Detailed explanation of PHP operator priority order

Apr 19, 2017 pm 04:37 PM

When we talked about PHP logical operators earlier, we mentioned the priority of PHP operators. The so-called operator priority refers to which operator in the expression is calculated first and which one is calculated later. It seems that the result

of the expression 1 + 5 * 3 is 16 instead of 18 because the multiplication sign ("*") has a higher priority than the plus sign ("+"). If necessary, parentheses can be used to force a priority change. For example: (1 + 5) * 3 has the value 18.

The rules followed by PHP operators in operations are: operations with higher priority are executed first, and operations with lower priority are executed later. If they have the same priority, they are executed in order from left to right, for example , "-" is a left-joint, then 1 - 2 - 3 is equivalent to (1 -

2) - 3 and the result is -4. On the other hand, "=" is a right-joint, so $a = $b = $c is equivalent to $a = ($b = $c).

Detailed explanation of PHP operator priority orderThe operators in the brackets are executed first. The use of brackets, even if it is not necessary, clearly indicates the order of operations through the pairing of brackets, rather than relying on operator priority and associativity. To decide, it can improve the readability of the code.

The table lists operators from high to low precedence. Operators in the same line have the same precedence, and the direction in which they are combined determines the order of evaluation.

/ %-.Bitwise OperatorNoneComparison operators                                  ;=>^|&&Logical operatorComparison operatorTernary operator             +=                                                   ‐ ##                     |=                                           ‐ or Logical operatorsWith so many priority levels, it is unrealistic to remember them all clearly. Yes, it’s not necessary. If the expression you write is very complex and contains a lot of running symbols, don’t use parentheses. Think of it like this:
<?php
$a and (($b !=$c) or (5*(50-$d)));
?>
Copy after login
Combining direction Operator Additional information
None clone new clone and new
left [ array ()
Right ** Arithmetic operator
Right ##++                 --           ~                 (int)                           ‐ #(string)           (array)               (object)                                                             ’ ’s ’ s         ’   ‐ ‐ P P S P C P C P C C P S C P S C S P S C S P S P S P S P S P S P S P S P S P S P S P S P D S N M /DecreasingNoneinstanceof Type
right ! Logical operator
left *                      
Arithmetic operator left+                          
Arithmetic operators and string operators ##left           >>
                                                                                                    >=
None==         !=           ===!==
Comparison operator left&bit operation Symbols and references left
bit operators left
bit operator left
Logical operator ##left ||
left ??
left ? :
Right =
-=*=           **=&=
^=#Assignment operatorleftandLogical operatorleftxorlogical operatorleft
This will reduce the possibility of logic errorsSometimes using parentheses can enhance the readability of the code. For example, the following:
<?php
$a = 3 * 3 % 5; // (3 * 3) % 5 = 4

//三元运行的结合
$a = true ? 0 : true ? 1 : 2; // (true ? 0 : true) ? 1 : 2 = 2

$a = 1;
$b = 2;
$a = $b += 3; // $a = ($b += 3) -> $a = 5, $b = 5


?>
Copy after login

The above is the detailed content of Detailed explanation of PHP operator priority order. For more information, please follow other related articles on the PHP Chinese website!

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

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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to safely set high priority for apps in Windows 11? How to safely set high priority for apps in Windows 11? May 06, 2023 pm 06:28 PM

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

Top 8 Ways to Disable Notifications on Windows 11 (and 3 Tips) Top 8 Ways to Disable Notifications on Windows 11 (and 3 Tips) May 05, 2023 pm 12:49 PM

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

How to change priority in Task Manager in Windows 11 How to change priority in Task Manager in Windows 11 May 17, 2023 am 10:26 AM

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

How to turn productivity mode on or off for an app or process in Windows 11 How to turn productivity mode on or off for an app or process in Windows 11 Apr 14, 2023 pm 09:46 PM

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

What is the root operator in C language? What is the root operator in C language? Mar 06, 2023 pm 02:39 PM

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.

Golang error: 'invalid use of ... operator' How to solve it? Golang error: 'invalid use of ... operator' How to solve it? Jun 24, 2023 pm 05:54 PM

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

How to customize notification settings on Windows 11 How to customize notification settings on Windows 11 May 02, 2023 pm 03:34 PM

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.

Detailed explanation of Linux process priority adjustment method Detailed explanation of Linux process priority adjustment method Mar 15, 2024 am 08:39 AM

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

See all articles