php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #8972 Referenced variables and unset()
Submitted: 2001-01-28 19:40 UTC Modified: 2001-03-08 12:06 UTC
From: aulbach at unter dot franken dot de Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 4.0.3pl1 OS: Linux
Private report: No CVE-ID: None
 [2001-01-28 19:40 UTC] aulbach at unter dot franken dot de
I found another bug (see id #8937) in connection with unset(). [Unset dosn?t work properly since august 1999]

<?

function test(&$bla) {
   unset($bla);
   $bla="huhu";
}

$bla="HIHI";

test($bla);

echo $bla;

?>

This will return "HIHI" instead of an expected "" or "huhu".

This bug exists cause unset() unsets the reference to $bla, not $bla itself. This also depends the GLOBAL-statement (see #8937)!

This is a bug, cause it is against the PHP-concept of making no big difference between reference and value
[ $bla=1; $hugo=&$bla; $hugo=2 is in the sight of $bla the same as $bla=1; $bla=2; ]

If I want to delete &$bla the correct expression is unset(&$bla). unset($bla) has always to delete the value
and all references to it. (that's just my opinion :-)

A fix of this bug will produce some compatibility problems with scripts, which has been written into PHP4 (cause the bug is very old).
But a fix should be done, cause many developers won't mind, that unseting a reference is in the sight of PHP a completly other thing than unseting a value.
There should be no/very less problems with upgrading PHP3-scripts.

PS: Just a small thought. In autumn last year I programmed constructs like the following
<?
$bla=array(1,array(1,array(1,2,3),3),3);
$ref=&$bla[2][2];
unset($ref);
?>
And I wondered, that this won't work as expected. Now I know why.
A correct solution would be to unset the inner array(1,2,3) and $ref... any other behaviour will break the mentioned handling of PHP.

Thougth 2:
<?
$bla="HIHI";
$ref=&$bla;
unset($ref);
$ref="huhu";
?>
What is the correct value of $bla. "" (empty) or "huhu"?

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-03-06 08:12 UTC] stas@php.net
No, it won't work that way (your first example). Unset
breaks the reference. Please read the "References explained"
part in the manual.
 [2001-03-08 11:53 UTC] aulbach at unter dot franken dot de
Dosn't solve my problem and the fact, that PHPs behavior is unuseable doing the thing I need it for. I have been written
an ugly workarround in PHP to make PHP working like I would expect it to.

During this I found out some other things, so in my eyes it remains as a bug, cause

<?
function test(&$bla) {
   unset($bla['hugo']);
   $bla['hugo']=3;
}

$bla=Array(
        'hugo'=>1,
        'huba'=>2
        );

test($bla);

print_r($bla);

?>

works as expected (Array ( [huba] => 2 [hugo] => 3 ) - what was the *least* thing I expected). So PHP works not harmonious with referenced variables, which is really a thing I cannot use in any way and will by sure cause many unexpected bugs for other developers - even if they read the documented behaviour for references very carefully.

Please would you be so kind to change the state of this bug (sorry, it IS a bug) to closed when PHP solves this problem as nearly everybody would expect it to; cause otherwise there could be the danger, that there will be no chance to change PHPs behavior, cause everybody expect it to behave like the mentioned bug. :-)

If you don't agree with me, just read the user contributed notes for 'References Explained'.
 [2001-03-08 12:06 UTC] stas@php.net
See, the PHP references work the certain way. You may like
it or not like it, but there's the way it works. If it
doesn't work as you expected, that's not a bug. A bug is
when the code is meant to do one thing and in fact does
other thing. The code in question is meant to do exactly
what it does. If you don't like it, you may discuss it with
Zend makers or in PHP-DEV, but there's no slightest point of
polluting bud database with things that are not bugs and
will be never regarded as bugs. If you have some other
concept of what references should be in PHP, you are welcome
to discuss it on the lists, but bug reporting system is not
a place for this.

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 19:01:28 2024 UTC