More concise ternary operators in PHP?:

Source: Internet
Author: User
Original address even shorter ternary operators in PHP using?:

Today, I found a small usage of the PHP ternary operator. It gives me a little fun with the dry brain!

The PHP ternary operator is a concise primary use when assigning values to a parameter. A major usage: the PHP ternary operator allows you to describe the decision code in a single line of code, replacing code similar to the following:


   
    if (isset($value)) {    $output = $value;} else {    $output = 'No value set.';}

Use the following code instead:


   
    $output = isset($value) ? $value : 'No value set.';

The second code example is a very concise usage, in many cases (not all), which is a very useful usage. There are many arguments about whether ternary operators should be used; Let me say that this is a tool that, like other tools, is just right or not.

What is the usual syntax (expression)? Value if Truthy:value if falsy. The expression can be a variable that tests whether the variable is true or false:


   
    $output = $value ? $value : 'No value set.';

The problem is: The above examples are very common and repetitive and somewhat annoying: write two times $value as if it were a mistake.

Fortunately, I found today that PHP 5.3 a more concise syntax for using the ternary operator is described in. You can learn from the manual, but here's how we make the above example more concise:


   
    $output = $value ?: 'No value set.';

This looks familiar, because it is much like the other shorthand operators:


   
    $value = $value . $other_value;

Converted into:


   
    $value .= $other_value;

In order to be more concise, this means that we can do it in such a way that it doesn't mean that we should write that. However, when we write concise code, this approach will look clearer and we should write this (and this feature allows us to use this operator in a variety of situations [this feature allows us to DRY up the ternary operator In many cases])

The above introduces the more concise ternary operators in PHP?:, including the content, I hope to be interested in the PHP tutorial friends helpful.

  • Related Article

    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.