matthewtbaker Posted May 13, 2013 Share Posted May 13, 2013 Just a thought... Is it possible to unset a global variable/array from within a function? So basically I want to pass an array to a function to convert to a string then destroy the original array. I'd like to keep it all contained within the function to reduce any additional clear-up scripting required. Here's my example code below: $GLOBALARRAY = array("keyname1" => "value1", "keyname2" => "value2"); $newstring = convertArrayToString($GLOBALARRAY, true); function convertArrayToString($array, $unset = false) { $string = ''; //Loop for each array value foreach ($array as $key => $value) { $string = $string . $key . '=' . $value ';'; } if ($unset === true) { unset($GLOBALARRAY_WHICH_WAS_PASSED); } return $string; } My guess is that I need to either get the name of the passed array or pass by reference? Thanks for your thoughts. Matt Link to comment https://forums.phpfreaks.com/topic/277954-unset-variablearray-which-has-been-passed-to-function/ Share on other sites More sharing options...
KevinM1 Posted May 13, 2013 Share Posted May 13, 2013 Why would you ever want to do that? Link to comment https://forums.phpfreaks.com/topic/277954-unset-variablearray-which-has-been-passed-to-function/#findComment-1429855 Share on other sites More sharing options...
Dathremar Posted May 13, 2013 Share Posted May 13, 2013 If you want you in-function changes to have effect on a global variable, like You said pass that variable by reference inside the function and change it. Link to comment https://forums.phpfreaks.com/topic/277954-unset-variablearray-which-has-been-passed-to-function/#findComment-1429860 Share on other sites More sharing options...
matthewtbaker Posted May 14, 2013 Author Share Posted May 14, 2013 On 5/13/2013 at 5:19 PM, Dathremar said: If you want you in-function changes to have effect on a global variable, like You said pass that variable by reference inside the function and change it. But changing an array to null for instance isn't the same as unset, is it? $array=null; Link to comment https://forums.phpfreaks.com/topic/277954-unset-variablearray-which-has-been-passed-to-function/#findComment-1429989 Share on other sites More sharing options...
Strider64 Posted May 14, 2013 Share Posted May 14, 2013 Null wipes the array, giving it a clean slate. Where as unset you can be selective in what you clear from the array. For Example: unset($row['salt']); unset($row['password']); Link to comment https://forums.phpfreaks.com/topic/277954-unset-variablearray-which-has-been-passed-to-function/#findComment-1429995 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.