php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #2589 reference inconsistency
Submitted: 1999-10-22 12:33 UTC Modified: 1999-10-23 16:00 UTC
From: hholzgra at media-engineering dot de Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 4.0 Beta 2 OS: redhat 6.0
Private report: No CVE-ID: None
 [1999-10-22 12:33 UTC] hholzgra at media-engineering dot de
Assigning by reference to a variable passed by reference
to a function works only inside the function but not
outside

<?

function testme(&$ref1,&$ref2)
{
  global $top;

  $ref1 = $top;
  $ref2 = &$top;

  print "top:'".$top.
        "' ,ref1:'".$ref1.
        "' ,ref2:'".$ref2.
        "'<br>";
}


$top = "hallo";
$x="x is unchanged";
$y="y is unchanged";

testme($x,$y);

print "top:'".$top."', x:'".$x."', y:'".$y."'<br>";

?>

prints 

top:'hello' ,ref1:'hello' ,ref2:'hello'
top:'hello', x:'hello', y:'y is unchanged'

instead if the expected 

top:'hello' ,ref1:'hello' ,ref2:'hello'
top:'hello', x:'hello', y:'hello'


--- 8< --- configure'd with:

./configure --with-mysql --enable-track-vars --with-imap

--- 8< --- php.ini looks like this

[PHP]
engine                  =       On
short_open_tag          =       On
asp_tags                =       Off
precision               =       14
y2k_compliance          =       Off
safe_mode               =       Off
highlight.string        =       #DD0000
highlight.comment       =       #FF8000
highlight.keyword       =       #007700
highlight.bg            =       #FFFFFF
highlight.default       =       #0000BB
highlight.html          =       #000000
allow_builtin_links     =       Off
max_execution_time      =       30
memory_limit            =       8388608
error_reporting         =       7
display_errors          =       On
log_errors              =       Off
track_errors            =       Off
warn_plus_overloading   =       Off
magic_quotes_gpc        =       Off
magic_quotes_runtime    =       Off
magic_quotes_sybase     =       Off
track_vars              =       Off

[Syslog]
define_syslog_variables =       Off


[mail function]
SMTP                    =       localhost
sendmail_from           =       me@localhost.com
sendmail_path           =       /usr/lib/sendmail -t

[Debugger]
debugger.host           =       localhost
debugger.port           =       7869
debugger.enabled        =       False

[SQL]
sql.safe_mode           =       Off


[MySQL]
mysql.allow_persistent  =       On
mysql.max_persistent    =       -1
mysql.max_links         =       -1


[Session]
session.save_handler    = files
session.save_path       = /tmp
session.name            = PHPSESSID
session.auto_start      = 0
session.lifetime        = 0
session.serialize_handler = php
session.gc_probability = 10
session.gc_maxlifetime = 1800

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [1999-10-23 16:00 UTC] andi at cvs dot php dot net
OK this is a tough one. The expression $ref = &$var changes the label "ref" to point to the value of $var. In your example, ref2 in the beginning of the function call is a reference to $y. After the command, $ref2 = &$top, the label "ref2" is changed to point at $top. The $ref = &$var construct doesn't change values, it just makes the lvalue variable point at another variable. As in the beginning of the function $ref1 and $x point at the same value, when you assign to $ref1, $x also changes. However, after $ref2 is reassigned to point at $top it no longer points at the previous value it pointed to $top.
I hope you understand the explanation of why this isn't a bug. It's a bit complicated and I'm not too good at explaining such things :)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 13:01:29 2024 UTC