Simple test questions about php operator precedence

巴扎黑
Release: 2023-03-14 18:26:01
Original
2022 people have browsed it

Someone in the group happened to be asking a question about the priority of PHP operators today. I happened to have encountered similar problems before. Let’s sort them out here so that everyone can understand and prevent mistakes:)

Let’s start with a simple one:

$a = 3;

if ($a = 5) {
$a++;
}
var_dump($a);// (int)6
So a good coding practice is to put the number in front and write it as 5 == $a. In this way, even if you write 5 = $a, an error will be reported at this time and it will not be an implicit assignment
Come on, let’s move on to the next pit.
$x = 2;
echo $x == 2 ? 'I' : $x == 1 ? 'you' : 'it'; //Your
classmates who fell into the trap may think that the result is me, because $x is indeed equal to 2 according to the rules of the ternary operator ?If the former is true, it will output me.
The knowledge point here lies in the operator priority and the left-association characteristics of the ternary operator. In fact, the real execution process is like this:
$x = 2;//Assign 2 to variable x Nothing to say
echo $x == 2 ? 'I' : $x == 1 ? 'You ' : 'It'; //You
//Because == has a higher priority than the ternary operator, it is converted into the following code
#echo true? 'I' : false ? 'You' : 'It';
//Due to the left associative nature of the ternary operator, the above code is equivalent to
# echo (true ? 'I' : false) ? 'You' : 'It';
//First calculate the
#echo 'I' in the left bracket ? 'You' : 'It'; //Everyone should understand by now
Well, continue to watch the pit
$a = 3;
$b = 5;
if ($a = 3 || $b = 7) {
$a++;
$b++;
}
var_dump($a);//(bool)true
var_dump($ b);//(int)6
I believe many students will have encountered this question when doing interview questions. I was also fooled the first time I did it.
The students who were tricked believe that there are probably several answers (4,6), (2,6)
$bThere is basically no controversy, the key is$ What is the situation? Let’s explain this pitfall.
#There is nothing to say about assignment
$a = 3;
$b = 5;
if ($a = 3 || $b = 7) {
$a++;
$b++;
}
//Knowledge point 1: || has a higher priority than
//So $a = 3 || $b = 7 is equivalent to $a = (3 || $b = 7)
//Because 3 || $b = 7, the entire expression is true, so it returns true and assigns it to $a. At this time, $a = true, it will be executed. Content in brackets
//Knowledge point 2: Increment/decrement operators do not affect Boolean values. Decreasing the NULL value has no effect, but the result of incrementing NULL is 1
//which means $a is still true, not 2 after 1++ $b is still++
//So the result is $a is true, $b is 6

after the auto-increment

Students who fall into the trap here will think it is 3. Note that the = sign is an assignment == comparison operator

The above is the detailed content of Simple test questions about php operator precedence. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!