Expression echo 5? : 1; Why output 5

Source: Internet
Author: User
Is this output null (empty)?

Reply content:

Is this output null (empty)?

Is the simple notation of the ternary operator, 5 is the true output of 5.

The document has an explanation: ternary operator

An expression (EXPR1)? (EXPR2): (EXPR3) when EXPR1 evaluates to TRUE, the value is EXPR2, and the value is EXPR1 when the EXPR3 evaluates to FALSE.
自 PHP 5.3 起,可以省略三元运算符中间那部分。表达式 expr1 ?: expr3 在 expr1 求值为 TRUE 时返回 expr1,否则返回 expr3。

Since 5 is greater than 0, returning the true,php ternary expression when the second argument is omitted, the result is true, and the first argument is returned.

a ? a : b;//可以缩写为a ?: b;//所以上面句 句子等效为:echo  5 ? 5  : 1 ;

A?: b; , if A is an expression, the expression is not executed again, and a? A:B; If an expression returns ture, it is executed again

Because 5 is true.

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