Bug #64248 [Com]: Strange parse error when using language construct in for
Edit report at https://bugs.php.net/bug.php?id=64248&edit=1
ID: 64248
Comment by: nikic@php.net
Reported by: bobwei9 at hotmail dot com
Summary: Strange parse error when using language construct in
for
Status: Not a bug
Type: Bug
Package: Scripting Engine problem
Operating System: Irrelevant (OS X 10.8)
PHP Version: master-Git-2013-02-19 (Git)
Block user comment: N
Private report: N
New Comment:
Imho unset() should stay as it is, a statement. It has no meaningful return value and does not
really benefit much from allowing expression use.
If you want to perform some complex operation in the last for-loop expressions, then you should just
put it into the body:
for ($i = 'A'; ++$i < $max;) {
var_dump($$i);
unset($$i);
}
The last for-loop expression is usually used for variable increments or other simple expressions.
It's not there to inline your whole loop body. If you want to put your unset() call in there,
why not move the var_dump in there too?
for ($i = 'A'; ++$i < $max; var_dump($$i), unset($$i));
Look, it's shorter!
Just my opinion on this.
Previous Comments:
------------------------------------------------------------------------
[2013-02-20 08:06:27] bobwei9 at hotmail dot com
It is a language construct, yes. But this seems to be a design mistake which should not be.
As in the pull request proposed: "an idea would be to make unset return true if the variable
has
existed else false?"
Does this need a RFC or a discussion on the internals?
------------------------------------------------------------------------
[2013-02-19 23:46:00] johannes@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
unset() jsut as many other language constructs has no return value and is no expression. for expects
expressions. This construct makes sense only in very few cases, even though it might make the
language a tiny bit simpler (less exceptions) it is not worth changing the language ... (changing
language has effects for the whole environment, from IDEs to code analyzers, to ...)
------------------------------------------------------------------------
[2013-02-19 21:05:21] bobwei9 at hotmail dot com
Oops, the expected result should be a notice and $max should be 'A' in the test script...
------------------------------------------------------------------------
[2013-02-19 21:03:57] bobwei9 at hotmail dot com
Description:
------------
For example unsetting a var in the third part of a for-loop throws an E_PARSE error.
Test script:
---------------
php -r '
$A = [1];
$B = [1,7];
$max = 'B';
for ($i='A'; ++$i<$max; unset($$i))
var_dump($$i);'
Expected result:
----------------
array(1) {
[0]=>
int(1)
}
array(2) {
[0]=>
int(1)
[1]=>
int(7)
}
Actual result:
--------------
PHP Parse error: syntax error, unexpected 'unset' (T_UNSET), expecting ')' in
Command line code on line 1
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=64248&edit=1
Thread (5 messages)