|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2013-02-19 21:03 UTC] 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
PatchesPull Requests
Pull requests:
HistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Nov 29 17:00:01 2025 UTC |
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.