: This article mainly introduces the more concise ternary operators in php? : If you are interested in the PHP Tutorial, refer to it. Original article: Even shorter ternary operators in PHP using? :
Today I found a small usage of the PHP ternary operator, which gives me a bit of fun to dry up my brain!
The PHP three-element operator is a simple usage when assigning values to parameters. A major usage: the PHP ternary operator allows you to describe the judgment code in a line of code and replace the code similar to the following:
Use the following code to replace:
The second code example is a very simple usage. in many cases (not all), this is a very practical usage. there are a lot of arguments about whether or not the ternary operators should be used. let me say that this is a tool. Like other tools, it is only true or not.
The commonly used syntax is (expression )? Value if truthy: value if falsy. This expression can be a variable to test whether the variable is true or false:
The problem is: the above examples are often used and repetitive, which is annoying: writing $ value twice is like making a mistake.
Fortunately, today I foundPHP 5.3This section introduces a more concise syntax for using the ternary operators. you can learn from the manual, but how can we make the above example more concise here:
This seems familiar because it is very similar to other short operators:
Convert:
For the sake of simplicity, this means that we can write the code in this way, but it does not mean that we should write the code in this way. However, when we write the code in a concise way, this method will look clearer and we should write it like this,(And this feature allows us to use this operator in many cases [this feature allows us to DRY up the ternary operator in memory cases])
Which of the following is a more concise ternary operator in php? :, Including some content, hope to be helpful to friends who are interested in PHP tutorials.