Expression echo5? : 1; why does output 5?

Source: Internet
Author: User
Isn't the output null (null? Isn't the output null (null?

Reply content:

Isn't the output null (null?

It is a simple way to write a ternary operator. 5 is output if it is true.

This document provides an explanation for the ternary operators.

Expression (expr1 )? (Expr2): (expr3) when the value of expr1 is TRUE, the value is expr2, and when the value of expr1 is FALSE, the value is expr3.
From PHP 5.3, the portion in the middle of the ternary operator can be omitted. Expression expr1? : Expr3 returns expr1 when the value of expr1 is TRUE; otherwise, expr3 is returned.

If the value of 5 is greater than 0, true is returned. If the php ternary expression ignores the second parameter and the result is true, the first parameter is returned.

A? A: B; // can be abbreviated as? : B; // so the above sentence is equivalent to echo 5? 5: 1;

A? : B;, if expression a is the expression, the expression will not be executed again, and? A: B; if the expression returns true, It will be 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.