php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #11466 it works ambiguously to unset the global variable in function
Submitted: 2001-06-13 11:14 UTC Modified: 2001-06-13 15:06 UTC
From: vanity at neowiz dot com Assigned:
Status: Not a bug Package: Variables related
PHP Version: 4.0.4pl1 OS: FreeBSD 3.5.1
Private report: No CVE-ID: None
 [2001-06-13 11:14 UTC] vanity at neowiz dot com
These two sources are the same except the last line of the second one.

The former represents that it doesn't work to unset the global variable,
the latter that it only works when unset throught the $GLOBALS variable.

[SRC 1: unset doesn't work]
============================================================
<?php
function unset_variable ($name, $flag)
  {
    global $$name;
    global $GLOBALS;

    if ($flag & 1)
      {
        echo "  - <font color=magenta>unset \$$name</font><br>\n";
        unset($$name);
      }
    if ($flag & 2)
      {
        echo "  - <font color=magenta>unset \$GLOBALS[\"$name\"]</font><br>\n";
        unset($GLOBALS[$name]);
      }
  }

function disp_variable ($name)
  {
    global $$name;
    global $GLOBALS;

    printf("  - \$%s : %s<BR>\n",
        $name, isset($$name) ? "<font color=green>".$$name."</font>" : "<font color=red>(unset)</font>");
    printf("  - \$GLOBALS[%s] : %s<BR>\n",
        $name, isset($GLOBALS[$name]) ? "<font color=green>".$GLOBALS[$name]."</font>" : "<font color=red>(unset)</font>");
  }

for ($i=0; $i < 4; $i++)
  {
    $name = "var$i";
    $$name = "VALUE_$i";
    $step = $i;
    $flag = $i;

    printf("<font color=blue>STEP (%s)</font><BR>\n", $step);

    disp_variable ($name);
    unset_variable ($name, $flag);
    disp_variable ($name);

    printf("<BR>\n");
  }
?>
============================================================

[SRC 2: unset works only throught the $GLOBALS variable]
============================================================
<?php
function unset_variable ($name, $flag)
  {
    global $$name;
    global $GLOBALS;

    if ($flag & 1)
      {
        echo "  - <font color=magenta>unset \$$name</font><br>\n";
        unset($$name);
      }
    if ($flag & 2)
      {
        echo "  - <font color=magenta>unset \$GLOBALS[\"$name\"]</font><br>\n";
        unset($GLOBALS[$name]);
      }
  }

function disp_variable ($name)
  {
    global $$name;
    global $GLOBALS;

    printf("  - \$%s : %s<BR>\n",
        $name, isset($$name) ? "<font color=green>".$$name."</font>" : "<font color=red>(unset)</font>");
    printf("  - \$GLOBALS[%s] : %s<BR>\n",
        $name, isset($GLOBALS[$name]) ? "<font color=green>".$GLOBALS[$name]."</font>" : "<font color=red>(unset)</font>");
  }

for ($i=0; $i < 4; $i++)
  {
    $name = "var$i";
    $$name = "VALUE_$i";
    $step = $i;
    $flag = $i;

    printf("<font color=blue>STEP (%s)</font><BR>\n", $step);

    disp_variable ($name);
    unset_variable ($name, $flag);
    disp_variable ($name);

    printf("<BR>\n");
  }
$GLOBALS[anything];
?>
============================================================

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-06-13 13:32 UTC] sniper@php.net
Yes, this is how it works, not a bug.


 [2001-06-13 15:00 UTC] vanity at neowiz dot com
I think that it's a bug even if this is how it works. :(
'Cause unset can does or doesn't work like this.

function unset_test ()
{
  global $var;
  global $arr;

  unset($var);               // unset works
  unset($arr["something"]);  // unset doesn't works
}

 [2001-06-13 15:06 UTC] vanity at neowiz dot com
oops, sorry.

  unset($var);               // unset doesn't works
  unset($arr["something"]);  // unset works

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Dec 30 14:01:28 2024 UTC