|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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 ')'
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 04:00:01 2025 UTC |
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!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).