What is the more concise ternary operator in php? :

Source: Internet
Author: User
: 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.