Home > Backend Development > PHP Problem > Detailed explanation of PHP operator precedence

Detailed explanation of PHP operator precedence

醉折花枝作酒筹
Release: 2023-03-09 09:48:02
Original
5425 people have browsed it

In php, an operation may have multiple operators connected together. We determine the order of execution of the operation. Below, the editor will introduce the php operator priority to everyone. I hope it will be helpful to friends who need it. help!

Detailed explanation of PHP operator precedence

The rule followed in PHP operations is: operators with high priority are executed first, and operators with low priority are executed later. In the case of the same priority, execution is from left to right. Of course, you can also use parentheses like mathematical operations, and the operators in the parentheses are executed first.

The following table lists the operators in PHP in order of priority from high to low. Operators in the same line have the same precedence, and their combination direction determines the direction of the order of operations.

Detailed explanation of PHP operator precedence

<?php
$bool = true && false;
var_dump($bool); 

$bool = true and false;
var_dump($bool); 
?>
Copy after login

His result is:

false
true
Copy after login
<?php
$a = 7 * 4 / 2; // (7 * 3) / 2 
echo $a;
$a = true ? 0 : true ? 1 : 7; // (true ? 0 : true) ? 1 : 2 
echo $a;

$a = 2;
$b = 7;
$a = $b -= 3; // $a = ($b -= 3) 
echo $a;
echo $b;
?>
Copy after login

The running result is:

14
7
4
4
Copy after login

It can be seen from the above three examples that for For operators in the same column, left represents operations from left to right, and right represents operations from right to left. If the combination direction is not applicable, it means that they cannot be used together.

Related recommendations:

2021 PHP interview questions summary (collection)

php video tutorial

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

Related labels:
php
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