php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #19146 Old Value kept after unset() on a &$parameter inside function.
Submitted: 2002-08-28 09:17 UTC Modified: 2002-09-04 12:25 UTC
From: purodha dot blissenbach at hostsharing dot net Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 4.1.2 OS: Linux, diverse Debian
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: purodha dot blissenbach at hostsharing dot net
New email:
PHP Version: OS:

 

 [2002-08-28 09:17 UTC] purodha dot blissenbach at hostsharing dot net
unset()ting a variable passed by reference does not destroy this variable, but rather protects it's value against being further altered inside the function.

< ? php
  function subroutine(&$byname, $assign, $unset)
  {
     if($assign) $byname = 'set by subroutine';
     if($unset)  unset($byname);  
     $byname = 'finally set inside subroutine';  
  }

   $byname = 'Set Outside!';
   subroutine($byname, TRUE, FALSE);
   echo ('<p>   1st. call: ($assign=TRUE,  $unset=FALSE) returns $byname="'.$byname.'"</p>');

   $byname = 'Set Outside!';
   subroutine($byname, FALSE, FALSE);
   echo ('<p>   2nd. call: ($assign=FALSE, $unset=FALSE) returns $byname="'.$byname.'"</p>');

   $byname = 'Set Outside!';
   subroutine($byname, TRUE, TRUE);
   echo ('<p>   3rd. call: ($assign=TRUE,  $unset=TRUE)  returns $byname="<b>'.$byname.'</b>"</p>');

   $byname = 'Set Outside!';
   subroutine($byname, FALSE, TRUE);
   echo ('<p>   4th. call: ($assign=FALSE, $unset=TRUE)  returns $byname="<b>'.$byname.'"</p></b>');
? >

Outputs:

   1st. call: ($assign=TRUE,  $unset=FALSE) returns $byname="finally set inside subroutine"
   2nd. call: ($assign=FALSE, $unset=FALSE) returns $byname="finally set inside subroutine"
   3rd. call: ($assign=TRUE,  $unset=TRUE)  returns $byname="set by subroutine"
   4th. call: ($assign=FALSE, $unset=TRUE)  returns $byname="Set Outside!"

More information and a live Demo can be obtained from 
http://purodha.hostsharing.de/hostsharing/menu0/test_by_name.php


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-09-04 12:25 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

unset() destroyes the $byname variable, so when it is re-created in the function it is no longer 'attached' to the $byname that is passed by reference to the function. Instead it is now a local variable inside that function.
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Wed Oct 07 08:00:02 2026 UTC