PHP is used if it is more logical OR (| |) When using the If judgment. The condition has no shorthand method

Source: Internet
Author: User
Like what

 $order['status'] == 9 || $order['status'] == 10 || $order['status'] == 11

Similar to this, if two or three is still acceptable, but if there are more than 3 conditions I can not accept the wording.
Later, the method of using In_array () was relatively brief.

Would like to ask the next PHP there is no better than In_array () the wording of a better point. Thank you

Reply content:

Like what

 $order['status'] == 9 || $order['status'] == 10 || $order['status'] == 11

Similar to this, if two or three is still acceptable, but if there are more than 3 conditions I can not accept the wording.
Later, the method of using In_array () was relatively brief.

Would like to ask the next PHP there is no better than In_array () the wording of a better point. Thank you

Sauce?

phpswitch($order['status']){    case FINISHED:    case PENDING:    case PROCESSING:        //logic        break;    default:        break;}

Isn't itin_array($order['status'], [9, 10, 11])

Like your example, change to switch on the line, simple and intuitive also reasonable;

If there are too many conditions to avoid, that is, if you can only use if you feel uncomfortable with something else, it is advisable to change the wording:

if ($order['status'] == 9     || $order['status'] == 10     || $order['status'] == 11) {}

I am a party of curly braces on another line, but in this case, you can also

if ($order['status'] == 9     || $order['status'] == 10     || $order['status'] == 11) {}

It is also advisable to put literal constants on the left.

If the judgment conditions in the If are particularly long (I've seen some branches that need to judge three or four variables, | | And && seven or eight), it is recommended to write the judging condition as a separate function

  • 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.