php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #64248 Strange parse error when using language construct in for
Submitted: 2013-02-19 21:03 UTC Modified: 2013-02-20 08:06 UTC
From: bobwei9 at hotmail dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: master-Git-2013-02-19 (Git) OS: Irrelevant (OS X 10.8)
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: bobwei9 at hotmail dot com
New email:
PHP Version: OS:

 

 [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

Patches

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-02-19 21:05 UTC] 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 23:46 UTC] johannes@php.net
-Status: Open +Status: Not a bug
 [2013-02-19 23:46 UTC] 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-20 08:06 UTC] 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-20 22:50 UTC] nikic@php.net
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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 19 14:01:32 2024 UTC