Bug #54740 [Com]: Ternary operator will not work with return by reference
Edit report at https://bugs.php.net/bug.php?id=54740edit=1 ID: 54740 Comment by: marrch dot caat at gmail dot com Reported by:dukeofgaming at gmail dot com Summary:Ternary operator will not work with return by reference Status: Not a bug Type: Bug Package:Scripting Engine problem PHP Version:Irrelevant Block user comment: N Private report: N New Comment: Mike, I understand that. The second note tells I caanot return a reference to an expression result, such as $object-method() or (new StdClass()) - I can understand that. But the code sample I provided doesn't try to do that. To make things even simplier, the following code still fails to compile: $link = $flag ? $a : $b; It doesn't try to return a reference to an expression, just a reference to a viriable; It doesn't try doing anything that the following code doesn't: if ($flag) $link = $a; else $link = $b; And maybi I'm really stupid, but after 10 years in PHP development I still don't understand why the first code cannot be compiled :( Previous Comments: [2013-10-02 05:27:05] m...@php.net I meant the documentation Note: (warning) not the user-contributed note. [2013-10-01 20:35:33] marrch dot caat at gmail dot com I thoroughly read the article you mentioned, Mike, but still don't understand why the following code fails to compile: $link = isset($i) ? ( $arr[$i]) : null; - while the following works fine: $link = $arr[$i]; In this case, $arr[$i] is a legal reference assignment, so the first code should behave equal to if (isset($i)) { $link = $arr[$i]; } else { $link = null; } - but this code works fine, and mentioned above isn't even compiled. What's wrong with it? [2013-10-01 14:42:02] m...@php.net Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php Check the second note here: http://php.net/manual/en/language.references.return.php [2012-08-27 14:17:44] marrch dot caat at gmail dot com This is a general problem with reference inside ternary operator. For ex., the following script fails with the same error: $link = isset($i) ? ( $arr[$i]) : null; - while the following works fine: $link = $arr[$i]; [2011-05-15 22:59:17] dukeofgaming at gmail dot com Description: PHP fails to parse a returned by reference value when using the ternary operator. The test script provided illustrates a case of when it is absolutely necessary to return by reference; if the is removed then the output would be a fatal error: Fatal error: Cannot use [] for reading in ... Test script: --- $value = ($condition)?( $some_value ):($object-Collection[]); Expected result: No errors, should be the equivalent of having: if($condition){ $value = $some_value; }else{ $value = $object-Collection[]; } Actual result: -- Parse error: syntax error, unexpected '' in ... -- Edit this bug report at https://bugs.php.net/bug.php?id=54740edit=1
Bug #54740 [Com]: Ternary operator will not work with return by reference
Edit report at https://bugs.php.net/bug.php?id=54740edit=1 ID: 54740 Comment by: ni...@php.net Reported by:dukeofgaming at gmail dot com Summary:Ternary operator will not work with return by reference Status: Not a bug Type: Bug Package:Scripting Engine problem PHP Version:Irrelevant Block user comment: N Private report: N New Comment: @marrch: PHP has no concept of a general operator that takes the reference of a variable(whatever that's supposed to be). PHP only has a = operator (which is really = and not a combination of = and ) which expects a variable on the right hand side. PHP also has a modifier for function arguments and return values. If you want to do yourself and others a favor, write $foo = $bar rather than $foo = $bar to make sure that there are no misunderstandings regarding this ;) Previous Comments: [2013-10-02 11:12:00] marrch dot caat at gmail dot com Mike, I understand that. The second note tells I caanot return a reference to an expression result, such as $object-method() or (new StdClass()) - I can understand that. But the code sample I provided doesn't try to do that. To make things even simplier, the following code still fails to compile: $link = $flag ? $a : $b; It doesn't try to return a reference to an expression, just a reference to a viriable; It doesn't try doing anything that the following code doesn't: if ($flag) $link = $a; else $link = $b; And maybi I'm really stupid, but after 10 years in PHP development I still don't understand why the first code cannot be compiled :( [2013-10-02 05:27:05] m...@php.net I meant the documentation Note: (warning) not the user-contributed note. [2013-10-01 20:35:33] marrch dot caat at gmail dot com I thoroughly read the article you mentioned, Mike, but still don't understand why the following code fails to compile: $link = isset($i) ? ( $arr[$i]) : null; - while the following works fine: $link = $arr[$i]; In this case, $arr[$i] is a legal reference assignment, so the first code should behave equal to if (isset($i)) { $link = $arr[$i]; } else { $link = null; } - but this code works fine, and mentioned above isn't even compiled. What's wrong with it? [2013-10-01 14:42:02] m...@php.net Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php Check the second note here: http://php.net/manual/en/language.references.return.php [2012-08-27 14:17:44] marrch dot caat at gmail dot com This is a general problem with reference inside ternary operator. For ex., the following script fails with the same error: $link = isset($i) ? ( $arr[$i]) : null; - while the following works fine: $link = $arr[$i]; The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at https://bugs.php.net/bug.php?id=54740 -- Edit this bug report at https://bugs.php.net/bug.php?id=54740edit=1
Bug #54740 [Com]: Ternary operator will not work with return by reference
Edit report at https://bugs.php.net/bug.php?id=54740edit=1 ID: 54740 Comment by: marrch dot caat at gmail dot com Reported by:dukeofgaming at gmail dot com Summary:Ternary operator will not work with return by reference Status: Not a bug Type: Bug Package:Scripting Engine problem PHP Version:Irrelevant Block user comment: N Private report: N New Comment: Thank you, Nikic! You've removed scales from my eyes. To my great pity and shame, I didn't understand that through all my working experience and really though is a take reference operator, as one as exists in C/C++ etc :( Thank you once again for your explanation! Now I see this is not really a bug... Previous Comments: [2013-10-02 11:20:20] ni...@php.net @marrch: PHP has no concept of a general operator that takes the reference of a variable(whatever that's supposed to be). PHP only has a = operator (which is really = and not a combination of = and ) which expects a variable on the right hand side. PHP also has a modifier for function arguments and return values. If you want to do yourself and others a favor, write $foo = $bar rather than $foo = $bar to make sure that there are no misunderstandings regarding this ;) [2013-10-02 11:12:00] marrch dot caat at gmail dot com Mike, I understand that. The second note tells I caanot return a reference to an expression result, such as $object-method() or (new StdClass()) - I can understand that. But the code sample I provided doesn't try to do that. To make things even simplier, the following code still fails to compile: $link = $flag ? $a : $b; It doesn't try to return a reference to an expression, just a reference to a viriable; It doesn't try doing anything that the following code doesn't: if ($flag) $link = $a; else $link = $b; And maybi I'm really stupid, but after 10 years in PHP development I still don't understand why the first code cannot be compiled :( [2013-10-02 05:27:05] m...@php.net I meant the documentation Note: (warning) not the user-contributed note. [2013-10-01 20:35:33] marrch dot caat at gmail dot com I thoroughly read the article you mentioned, Mike, but still don't understand why the following code fails to compile: $link = isset($i) ? ( $arr[$i]) : null; - while the following works fine: $link = $arr[$i]; In this case, $arr[$i] is a legal reference assignment, so the first code should behave equal to if (isset($i)) { $link = $arr[$i]; } else { $link = null; } - but this code works fine, and mentioned above isn't even compiled. What's wrong with it? [2013-10-01 14:42:02] m...@php.net Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php Check the second note here: http://php.net/manual/en/language.references.return.php The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at https://bugs.php.net/bug.php?id=54740 -- Edit this bug report at https://bugs.php.net/bug.php?id=54740edit=1
Bug #54740 [Com]: Ternary operator will not work with return by reference
Edit report at https://bugs.php.net/bug.php?id=54740edit=1 ID: 54740 Comment by: marrch dot caat at gmail dot com Reported by:dukeofgaming at gmail dot com Summary:Ternary operator will not work with return by reference Status: Not a bug Type: Bug Package:Scripting Engine problem PHP Version:Irrelevant Block user comment: N Private report: N New Comment: I thoroughly read the article you mentioned, Mike, but still don't understand why the following code fails to compile: $link = isset($i) ? ( $arr[$i]) : null; - while the following works fine: $link = $arr[$i]; In this case, $arr[$i] is a legal reference assignment, so the first code should behave equal to if (isset($i)) { $link = $arr[$i]; } else { $link = null; } - but this code works fine, and mentioned above isn't even compiled. What's wrong with it? Previous Comments: [2013-10-01 14:42:02] m...@php.net Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php Check the second note here: http://php.net/manual/en/language.references.return.php [2012-08-27 14:17:44] marrch dot caat at gmail dot com This is a general problem with reference inside ternary operator. For ex., the following script fails with the same error: $link = isset($i) ? ( $arr[$i]) : null; - while the following works fine: $link = $arr[$i]; [2011-05-15 22:59:17] dukeofgaming at gmail dot com Description: PHP fails to parse a returned by reference value when using the ternary operator. The test script provided illustrates a case of when it is absolutely necessary to return by reference; if the is removed then the output would be a fatal error: Fatal error: Cannot use [] for reading in ... Test script: --- $value = ($condition)?( $some_value ):($object-Collection[]); Expected result: No errors, should be the equivalent of having: if($condition){ $value = $some_value; }else{ $value = $object-Collection[]; } Actual result: -- Parse error: syntax error, unexpected '' in ... -- Edit this bug report at https://bugs.php.net/bug.php?id=54740edit=1
Bug #54740 [Com]: Ternary operator will not work with return by reference
Edit report at https://bugs.php.net/bug.php?id=54740edit=1 ID: 54740 Comment by: marrch dot caat at gmail dot com Reported by:dukeofgaming at gmail dot com Summary:Ternary operator will not work with return by reference Status: Open Type: Bug Package:Compile Failure PHP Version:Irrelevant Block user comment: N Private report: N New Comment: This is a general problem with reference inside ternary operator. For ex., the following script fails with the same error: $link = isset($i) ? ( $arr[$i]) : null; - while the following works fine: $link = $arr[$i]; Previous Comments: [2011-05-15 22:59:17] dukeofgaming at gmail dot com Description: PHP fails to parse a returned by reference value when using the ternary operator. The test script provided illustrates a case of when it is absolutely necessary to return by reference; if the is removed then the output would be a fatal error: Fatal error: Cannot use [] for reading in ... Test script: --- $value = ($condition)?( $some_value ):($object-Collection[]); Expected result: No errors, should be the equivalent of having: if($condition){ $value = $some_value; }else{ $value = $object-Collection[]; } Actual result: -- Parse error: syntax error, unexpected '' in ... -- Edit this bug report at https://bugs.php.net/bug.php?id=54740edit=1