PHP makes ternary operators easier to use.

Source: Internet
Author: User

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:

<?phpif (Isset ($value)) {    $output = $value;} else {    $output = ' No value set. ';}

Use the following code instead:

<?php$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:

<?php$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:

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

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

<?php$value = $value. $other _value;

Converted into:

<?php$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])

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:

<?phpif (Isset ($value)) {    $output = $value;} else {    $output = ' No value set. ';}

Use the following code instead:

<?php$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:

<?php$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:

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

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

<?php$value = $value. $other _value;

Converted into:

<?php$value. = $other _value;

For the sake of brevity, this means that we can do this shorthand, which does not mean we should write like this. However, when we write concise code, this way will look clearer, we should 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])

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.