php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #27964 foreach() + @ + with references broken
Submitted: 2004-04-12 14:32 UTC Modified: 2005-06-23 23:29 UTC
Votes:3
Avg. Score:4.7 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:1 (50.0%)
From: jeff at opendbms dot com Assigned: dmitry (profile)
Status: Wont fix Package: Scripting Engine problem
PHP Version: 5CVS-2005-06-19 OS: *
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2004-04-12 14:32 UTC] jeff at opendbms dot com
Description:
------------
When I run this code:

foreach (@$formErrors as &$formError) {

I get this:

parse error, unexpected '&', expecting T_STRING or T_VARIABLE or '$' 

Something about using both the @ and the & in the same foreach loop. It works with either one alone, just not both.

Reproduce code:
---------------
use the foreach on a non-existant array with references, and the @ statement modifier.

Expected result:
----------------
Should skip the foreach loop.

Actual result:
--------------
Doesn't work.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-04-12 19:41 UTC] sniper@php.net
# php -r '$array = array(); foreach(@$array as $key => &$value) {}'
Fatal error: Cannot create references to elements of a temporary array expression in Command line code on line 1

(remove @ -> no error)

# php -r '$array = array(); foreach($array as &$value) {}'
Fatal error: Key element cannot be a reference in Command line code on line 1

# php -r '$array = array(); foreach(@$array as &$value) {}'
PHP Parse error:  parse error, expecting `T_STRING' or `T_VARIABLE' or `'$'' in Command line code on line 1

 [2005-06-06 12:26 UTC] sniper@php.net
Dmitry, can you look into this?

 [2005-06-23 23:29 UTC] andi@php.net
Assigning by reference only works with variables and not expressions. As @ starts an expression (as it requires to start and stop error handling and therefore doesn't have a clean variable context) it makes it extremely hard to support.
This behavior is quite general in PHP where variables that start with $ have more capabilities than expressions.
Sorry...
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 08:01:29 2024 UTC