php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #10810 unset in function doesn't work on global arrays
Submitted: 2001-05-11 05:53 UTC Modified: 2001-05-11 05:55 UTC
From: Martin dot Sander at touch-screen dot de Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 4.0.5 OS: RedHat 7.0
Private report: No CVE-ID: None
 [2001-05-11 05:53 UTC] Martin dot Sander at touch-screen dot de
unset in function doesn't work on global arrays

example:

<?php
{
  function ListIt() {
    global $ar;
    if (is_array($ar)) {
      foreach($ar as $k => $v) {
	echo "ar[$k] = $v <br>\n";
      }
    } else {
      echo "empty/nonarray <br>\n";
    }
    echo "<br>\n";
  }

  function UnsetOne($i) {
    global $ar;
    unset($ar[$i]);
  }

  function UnsetAll() {
    global $ar;
    //echo "t1s: <br>\n"; ListIt(); echo ":t1e <br>\n";
    unset($ar);
    //echo "t2s: <br>\n"; ListIt(); echo ":t2e <br>\n";
  }

  echo "init: <br>\n";
  $ar = array();
  $ar[1] = "a";
  $ar[2] = "b";
  $ar[3] = "c";
  $ar[4] = "d";
  ListIt();

  echo "unset 3: <br>\n";
  UnsetOne(3);
  ListIt();

  echo "unset all (global array in function): <br>\n";
  UnsetAll();
  ListIt();

  echo "unset all (top level): <br>\n";
  unset($ar);
  ListIt();

}
?>

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-05-11 05:55 UTC] derick@php.net
Not a bug. The unset ($ar) in the unset all function removes the reference to the global variabele.
Use unset($GLOBALS["ar"]) if you want this to work.

Derick
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 14:01:29 2024 UTC