runeveryday Posted March 3, 2010 Share Posted March 3, 2010 <?php function test() { global $a; unset($a); } $a = 1; test(); print $a; ?> in the function test() i did unset($a); but when i print $a;there is a result 1, why ?any tips would be appreciated. Link to comment https://forums.phpfreaks.com/topic/193970-the-global/ Share on other sites More sharing options...
Dennis1986 Posted March 3, 2010 Share Posted March 3, 2010 <?php function test() { unset($GLOBALS['a']); } $a = 1; test(); print $a; ?> Destroys the global and prints nothing. Link to comment https://forums.phpfreaks.com/topic/193970-the-global/#findComment-1020799 Share on other sites More sharing options...
runeveryday Posted March 3, 2010 Author Share Posted March 3, 2010 why GLOBE['a'] can't output a result,but the global can,what's difference between the two ? the more detail,the better.thank you! Link to comment https://forums.phpfreaks.com/topic/193970-the-global/#findComment-1020869 Share on other sites More sharing options...
runeveryday Posted March 4, 2010 Author Share Posted March 4, 2010 who can help me? thank you! Link to comment https://forums.phpfreaks.com/topic/193970-the-global/#findComment-1021266 Share on other sites More sharing options...
PFMaBiSmAd Posted March 4, 2010 Share Posted March 4, 2010 global and $GLOBALS should not be used in functions anyway because that breaks the general purpose nature and encapsulation that functions are designed to provide. Link to comment https://forums.phpfreaks.com/topic/193970-the-global/#findComment-1021270 Share on other sites More sharing options...
runeveryday Posted March 4, 2010 Author Share Posted March 4, 2010 why not use global? if you want to use a variable which declare out of a function,but how to do it? Link to comment https://forums.phpfreaks.com/topic/193970-the-global/#findComment-1021291 Share on other sites More sharing options...
KevinM1 Posted March 4, 2010 Share Posted March 4, 2010 why not use global? if you want to use a variable which declare out of a function,but how to do it? Pass it to the function via the function's argument list? Example: function priceWithTax($price, $salesTax) { return $price + ($price * $salesTax); } $price = 5.00; $tax = 0.08; $finalPrice = priceWithTax($price, $tax); Link to comment https://forums.phpfreaks.com/topic/193970-the-global/#findComment-1021387 Share on other sites More sharing options...
PFMaBiSmAd Posted March 4, 2010 Share Posted March 4, 2010 You pass the value pass into the function as a parameter when you call the function - <?php function test($a) { $a = NULL; return $a; } $a = 1; $a = test($a); var_dump($a); ?> The above code passes the value in as a parameter and returns the result (you cannot actually use unset in this example like the original code was doing because $a would no longer exist and could not be returned.) If you actually want to unset a main program variable, I recommend just using unset() in your main code rather than making a function with it in it. Replacing a call to a built in function with a call to a user function that only contains a call to that built in function just adds unnecessary overhead to your program. Link to comment https://forums.phpfreaks.com/topic/193970-the-global/#findComment-1021391 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.