php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #35995 Problems defining vars in arguments
Submitted: 2006-01-13 12:49 UTC Modified: 2006-01-16 20:54 UTC
Votes:6
Avg. Score:4.3 ± 0.7
Reproduced:6 of 6 (100.0%)
Same Version:6 (100.0%)
Same OS:4 (66.7%)
From: jadelucca at comcast dot net Assigned: dmitry (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 5.1.2 OS: Windows XP
Private report: No CVE-ID: None
 [2006-01-13 12:49 UTC] jadelucca at comcast dot net
Description:
------------
You cannot define a variable in arguments in 5.1.2, but you could in previous versions.

Reproduce code:
---------------
AN EXAMPLE would be:

<?php

function prefix(&$text) {
	$text = "Prefix " . $text;
}

prefix($text = "Add prefix to this here, which would do that in older versions but not this one.");

?>

Expected result:
----------------
After I run the code above, $text *should* be "Prefix Add prefix..." etc. This is how it worked in older versions of PHP.

Actual result:
--------------
$text is unmodified. It passes-by-value, not reference, despite the fact that I added the & in the argument list. 

*Modification example*

If I swap the & sign from the function argument list to the actual function arguments, like so:

<?php

function prefix($text) {
	$text = "Prefix " . $text;
}

prefix(&$text = "Add prefix to this here, which would do that in older versions but not this one.");

?>

I get Parse error: syntax error, unexpected '=', expecting ')'

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-01-13 12:55 UTC] jadelucca at comcast dot net
Under reproduce code, that SAME EXACT CODE WORKS on PHP 5.1.1! I just testing.

The SECOND (Modification code) sends a parse error there too, so maybe the 2nd example can't be done.

But the bug still lies within PHP 5.1.2! (For those who speed-read through this and perhaps misundestood me.)
 [2006-01-13 12:56 UTC] tony2001@php.net
It's not possible to pass expressions by reference.
`$test = "default value"` is an expression.
This is expected behaviour.

 [2006-01-13 13:02 UTC] jadelucca at comcast dot net
tony

Thank you. I realize this now. BUT let's not give people the wrong impression, the bug still exists :) I shouldn't have even mentioned the 2nd part because it'll confuse people, confusing them with erroneous code and an example of the bug.
 [2006-01-13 13:15 UTC] jadelucca at comcast dot net
I don't mean to be a pest, I understand you all have other things to tend to, but please acknowledge this bug. 

Tony wasn't looking at the right code. The TOP code is the bug, not the bottom, which is what he commented on.

The TOP code WORKS in PHP 5.1.1 but not PHP 5.1.2. I'm 99.9% sure it also works in 4.4.1 but I'm not 100%; I don't have access to 4.4.1 now.
 [2006-01-13 17:50 UTC] tony2001@php.net
It doesn't work in 4.4.x either and it has never worked.
There is no bug and I've explained why.
 [2006-01-13 20:29 UTC] jadelucca at comcast dot net
Tony, I think you're a little confused.

This code:

<?php

function prefix(&$text) {
	$text = "Prefix " . $text;
}

prefix($text = "Add prefix to this here, which would do that in older
versions but not this one.");

?>

WORKS in PHP 5.5.1 but doesn't in PHP 5.5.2. One version has to have an error then. I know because I was doing that sort of thing on this script I was making and everything was working fine, but then when I upgraded to 5.5.2 it stopped working. I had to downgrade back to 5.5.1 and it works again.

ALSO, I just tested the above code and it works in 4.4.1!

Now, can I please get another PHP debugger? This one is a bit confused!
 [2006-01-13 20:36 UTC] jadelucca at comcast dot net
Hah, I meant 5.1.x in my above post.

Once again, the code works in all but 5.1.2 and results "Prefix add prefix"... There really should be an edit post here so I don't need to keep posting new messages :(
 [2006-01-13 23:48 UTC] jadelucca at comcast dot net
Modified to open. The bug still has not be acknowledged.
 [2006-01-14 15:09 UTC] sniper@php.net
It was already acknowledged that this is not a bug. Twice.
 [2006-01-14 19:38 UTC] jadelucca at comcast dot net
Tony and Sniper,

If this is not a bug, could you then tell me why it works in PHP 5.1.1 and PHP 4.4.1 but not in 5.1.2? Was there a bug in those two versions then? I still don't think you're seeing what I'm trying to say...
 [2006-01-14 19:59 UTC] derick@php.net
It's not a bug in 5.1.2, but in earlier versions. PHP 4.4 still inhbits this bug though, in the form that it doesn't give an E_STRICT error. Dmitry, can you have a look at that?
 [2006-01-14 21:47 UTC] jadelucca at comcast dot net
Thank you Derrick! I really appreciate your help. I will change my code not to do this... I thought you could since it issued no errors.
 [2006-01-15 19:26 UTC] jadelucca at comcast dot net
iliaa, did you read Derick's post? You're right; it's NOT a bug, in 5.1.2, that is. It is a bug in the 4.4.x version that it works in the first place without issuing errors.
 [2006-01-15 19:56 UTC] sniper@php.net
And you've reported it as PHP 5.1.2 bug which it STILL IS NOT.
Please stop reopening this, we're not gonna change it.
 [2006-01-15 22:16 UTC] derick@php.net
Jani, I discussed this with Ilia earlier today and it *is* a BC breaking change in PHP 5.1.2 - all other versions do not do this (including PHP 4.4.x or 5.1.1). Therefore it is a bug and it should be addressed. Dmitry, if you feel otherwise, please close it then.
 [2006-01-16 11:23 UTC] dmitry@php.net
The following code shouldn't be worked according to language specification, but worked before 5.1 because of side effect

<?php
function test(&$text) {
  $text = "Prefix " . $text;
}
test($x="A");
echo $x;
?>

However the following code never worked

<?php
function test(&$text) {
  $text = "Prefix " . $text;
}
$y="A";
test($x=$y);
echo $x;
?>

I think we shouldn't keep such mess for BC and break language specification (the assignment $x="A" in first example is an expression and it can not be passed by reference).
 [2006-01-16 20:54 UTC] jadelucca at comcast dot net
Then I suppose that's it. I closed it; I said what I had to say.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 10:01:28 2024 UTC