Home  >  Article  >  Backend Development  >  About a problem encountered by PHP conditional operator and its solution

About a problem encountered by PHP conditional operator and its solution

不言
不言Original
2018-07-14 13:54:211392browse

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
int main() {
  int x = 1;
  int shit = x == 1 ? 100 : 
     x == 2 ? 200 : 300;
  printf("shit的值:%d\n", shit);
  return 0;
}

Compile and run it

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

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):

Execute it:

root$ php test.php
shit的值:200

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;
is equivalent to

$shit = ($x == 1) ? 100 : 
   ($x == 2) ? 200 : 300;
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
  • 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

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);
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)))))));
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!

Statement:
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