About a problem encountered by PHP conditional operator and its solution

不言
Release: 2023-04-03 07:56:01
Original
1542 people have browsed it

This article mainly introduces a problem and solution encountered about PHP conditional operators. It has a certain reference value. Now I share it with you. Friends in need can refer to it.

Encountered today Come to a question about the nested use of conditional operators (ternary expressions) in PHP

Phenomena

Let’s first look at a piece of C language code (test.c):

#include<stdio.h>
int main() {
  int x = 1;
  int shit = x == 1 ? 100 : 
     x == 2 ? 200 : 300;
  printf("shit的值:%d\n", shit);
  return 0;
}</stdio.h>
Copy after login

Compile and run it

root$ gcc test.c -o test && ./test
shit的值:100
Copy after login

The answer is as expected, because x==1, so 100 is assigned to shit.

But if we rewrite the above code in PHP (test.php):

<?php $x = 1;
$shit = $x == 1 ? 100 : 
   $x == 2 ? 200 : 300;
echo "shit的值:$shit\n";
Copy after login

Execute it:

root$ php test.php
shit的值:200
Copy after login

We find that it returns The results are different, why is this?

Troubleshooting

First of all, we suspect that it may be a priority issue with comparison operator(==) and conditional operator(?:) in PHP , let’s check the PHP official documentation

##== has a higher priority than ?: (C language This is also true), so

$shit = $x == 1 ? 100 : 
   $x == 2 ? 200 : 300;
Copy after login
is equivalent to

$shit = ($x == 1) ? 100 : 
   ($x == 2) ? 200 : 300;
Copy after login
and it is true when executed once. The possibility of operator priority causing the problem can be ruled out.

But in the official document, there is such a sentence in the example of

operator combination direction:

This is very similar to the phenomenon described above, The problem should be here. After some research, I got the following conclusion:About a problem encountered by PHP conditional operator and its solution

Conclusion

  • The combination direction of the conditional operator (?:) in C language is

    from right to left, each evaluation starts from the rightmost subexpression, so

int x = 1;

int shit = x == 1 ? 100 : 
     x == 2 ? 200 : 300;
//等效于
int shit = x == 1 ? 100 : 
     (x == 2 ? 200 : 300);
//等效于
int shit = x == 1 ? 100 : 
     (300);// 100
Copy after login
  • combination of PHP’s conditional operator (?:) The direction is

    from left to right, and each evaluation starts from the leftmost subexpression, so

$x = 1;
$shit = $x == 1 ? 100 : 
   $x == 2 ? 200 : 300;
//等效于
$shit = ($x == 1 ? 100 : 
   $x == 2) ? 200 : 300;
//等效于
$shit = (100) ? 200 : 300;// 200
Copy after login

is between PHP Conditional operator combination direction, we cannot achieve the effect of if-elseif-elseif-else expression by nesting conditional operators like C/C, unless we add parentheses to the later sub-expressions , in this example it can be solved in this way:

$shit = $x == 1 ? 100 : 
   ($x == 2 ? 200 : 300);
Copy after login
However, when there are many conditional branches, code readability problems (stacked brackets) will occur:

$shit = $x == 1 ? 100 :
     ($x == 2 ? 200 :
     ($x== 3 ? 300 :
     ...
     ($x == 8 ? 800 : 900)))))));
Copy after login
Due to PHP's writing method of not stacking parentheses is inconsistent with C/C in terms of execution results, and the default combination direction can only be changed by adding parentheses to achieve the expected results, so the PHP document simply does not recommend the use of nested conditional operators:

Note:
It is recommended that you avoid "stacking" ternary expressions. PHP's
behaviour when using more than one ternary operator within a single statement is non-obvious
The above is The entire content of this article is hoped to be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Let’s talk about dependency injection, containers and appearance patterns in framework development (Part 2)

How to solve the problems of PHP Problems with concurrency and large traffic

The above is the detailed content of About a problem encountered by PHP conditional operator and its solution. For more information, please follow other related articles on the PHP Chinese website!

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