php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #38626 Passing not variables by Reference
Submitted: 2006-08-28 12:43 UTC Modified: 2006-08-28 16:27 UTC
Votes:3
Avg. Score:4.7 ± 0.5
Reproduced:3 of 3 (100.0%)
Same Version:2 (66.7%)
Same OS:2 (66.7%)
From: php at swapo dot de Assigned:
Status: Wont fix Package: Feature/Change Request
PHP Version: 5.1.5 OS: all
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: php at swapo dot de
New email:
PHP Version: OS:

 

 [2006-08-28 12:43 UTC] php at swapo dot de
Description:
------------
The behaviour of pass by reference is a little odd.

As stated here (http://www.php.net/manual/en/language.references.pass.php) passing by reference works only if you use a variable as a function argument.
This is also the case with builtin functions like array_pop but I can think of many situations when it makes sense to use a function's return value as an argument to another function.

A common example (perhaps not the best though) is to get a file extension by combining explode and array_pop.

This wasn't a problem until PHP 5.1 and I think it shouldn't be now.

Reproduce code:
---------------
// doesn't work
$extension = array_pop(explode('.', $filename));

// works
$parts = explode('.', $filename);
$extension = array_pop($parts);
unset($parts);

// surpressing the error message works too
$extension = @array_pop(explode('.', $filename));

Expected result:
----------------
All examples should give the same result.

Actual result:
--------------
The first example results in an error message as the argument for array_pop must be a variable:

Strict Standards:  Only variables should be passed by reference

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-08-28 13:33 UTC] tony2001@php.net
>$extension = @array_pop(explode('.', $filename));
explode(..) is not a variable, hence it can't be passed by reference.
 [2006-08-28 14:36 UTC] php at swapo dot de
I understand the reason as it is in the manual too. That's why I submitted this as a change request.

If you can give me a good reason (aside from php's internal "flaws") why my example is bad I'll accept that.

In my understanding a reference should be on the content not on a variable name. I guess that's the way it's done with objects since PHP5 and the way references are handled in other languages (no one would mind "someMethod(new SomeClass())" or "someMethod(getSomeObject())").
If this isn't possible with the scripting engine it'd still be enough to keep the old behaviour and silently ignore the & in that cases.

Backwards compatibility can't be the reason too as scripts that worked in the past generate errors now. BTW: That's the way I came across this issue: By migrating from 5.0 to 5.1.

Adding either two useless statments ("$var = some_function();" and "unset($var);" or an error suppressing "@" seems uneeded complicated to me.
 [2006-08-28 14:44 UTC] derick@php.net
It's not an "internal flaw" - it's how it is supposed to work. A reference works on a *variable* not only on its contents.
 [2006-08-28 16:27 UTC] php at swapo dot de
And I'd like to know a reason for this ...uhm... uncommon behaviour ;-)

"Because it is so" doesn't seem like a good reason to me.
Especially as it wasn't a problem in the past (although it may  not have beenn meant that way) and PHP5's object references work another way.

Seems a little unformed to me so far.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Mon Jul 07 10:01:34 2025 UTC