php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #35075 Assignment by reference using a function assignes the variable by value
Submitted: 2005-11-03 01:47 UTC Modified: 2005-11-03 16:37 UTC
From: skrol29 at freesurf dot fr Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.0.5 OS: Windows XP
Private report: No CVE-ID: None
 [2005-11-03 01:47 UTC] skrol29 at freesurf dot fr
Description:
------------
This bug is a change of Assignment by reference behavior bewteen Php versions <= 5.0.4 and the last 5.0.5.

In Php 5.0.5 when a variable is re-assigned by reference directly using a function, then the variable is assigned by value.  

Reproduce code:
---------------
<?php

$a = 'hello';
$b =& $a;       
$a =& f_test();

echo "* a = {".$a."}, b = {".$b."}<br>";

function f_test() {
	return 'tested';
}

?>

Expected result:
----------------
* a = {tested}, b = {hello}

Actual result:
--------------
* a = {tested}, b = {tested}

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-11-03 09:10 UTC] derick@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5-STABLE-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php5.0-win32-latest.zip
 [2005-11-03 10:24 UTC] skrol29 at freesurf dot fr
The same bug occurs with this last Php version (pompted version was 5.0.6-dev).
 [2005-11-03 15:50 UTC] iliaa@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

You cannot return a reference to a literal, if you rewrite your code in the following manner it works properly.

<?php
$a = 'hello'; $b =& $a;
$a = &f_test();

echo "* a = {".$a."}, b = {".$b."}\n";

function &f_test() {
        $a = 'tested';
        return $a;
}
?>
 [2005-11-03 16:37 UTC] skrol29 at freesurf dot fr
Hello,

That is correct, the manual says "The [assignement] syntax can be used with functions, that return references".
So the function should return reference to be assigned properly by reference to a variable.

Nevertheless it could be intersting to warn about that change of behavior which occurse on version 5.0.5. Because no PHP Error message neither Notice are prompted when the function doesn't return references.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu May 02 10:01:30 2024 UTC