0

I need to empty a global variable from withing a user-defined function. My understanding is that this should be possible assuming you set the global variable with [global] inside the user-defined function.

My attempts as below:

<pre>
<?php

$storage_clean_from_values = "yes";

$storage = [1,2,3];
var_dump($storage);

function clean_storage() {

    global $storage;

    if ($storage_clean_from_values == "yes") {
        $storage = [];
    }

}

clean_storage();

var_dump($storage);

Output

array(3) { # Outcome of first [var_dump].
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
}
array(3) { # Outcome of second [var_dump].
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
}

Wanted behaviour:

The first [var_dump] should show all the existing values in [$storage], the second [var_dump] should show the [$_storage] but content should be blank.

Toolbox
  • 2,333
  • 12
  • 26

0 Answers0