Is this a bug?

Source: Internet
Author: User
Is this a bug?
function &test(){    $data = 'test';        return (is_null($data) ? $data : $data);        if( is_null($data) )        return $data;    else        return $data;}$s = test();var_dump($s);

This is to describe my intention. the actual application is much more complicated. The function returns a reference. However, I found that if the three-object operator is used for expression, PHP cannot correctly understand return, and
PHP Notice:  Only variable references should be returned by reference

If is used, everything is normal.

I don't know if this is a bug, because many people have discovered it before, and I have encountered this problem in both 5.4.11 and 5.5.0alpha4.


Reply to discussion (solution)

Also found in the manual:
Note: the ternary operator is a statement. Therefore, the result is not a variable but a statement. It is important to return a variable through reference. Return $ var = 42? $ A: $ B; will not work, and a warning will be issued for future PHP versions.

Paste

This is not a BUG! It is a correct syntax analysis.
Your function & test () declares that the function returns a reference. Since it is a reference, there must be a carrier to be referenced.
The three-object operation is a structure, and the result must be assigned to real variables.
$ A = is_null ($ data )? $ Data: $ data;
Return $;
No errors
Of course, function test () does not cause errors.

Many early programs have had this problem since php5.3.

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.