jaymc Posted June 28, 2009 Share Posted June 28, 2009 For some reason when executing a function which uses global $variable_name the $variable_name is empty echo $clientID; // Outputs the value of $clientID fine credits_add("1", "6", "FREE", "0.00"); // Does not output $clientID echo $clientID; // Outputs the value of $clientID fine credits_add() function is shown below function credits_add($userID, $credits, $paymentType, $rate) { global $clientID; echo $clientID; } Quote Link to comment https://forums.phpfreaks.com/topic/164016-solved-global-not-working/ Share on other sites More sharing options...
dzelenika Posted June 28, 2009 Share Posted June 28, 2009 is you first part of code in global "namespace"? could you post entire function credits_add()? Quote Link to comment https://forums.phpfreaks.com/topic/164016-solved-global-not-working/#findComment-865242 Share on other sites More sharing options...
jaymc Posted June 28, 2009 Author Share Posted June 28, 2009 Its not just that function. Here is a brief layout of my code <? include_once(HOMEDIR."funcs/random_password.php"); include_once(HOMEDIR."funcs/copy_dbStructure.php"); include_once(HOMEDIR."funcs/credits_add.php"); include_once(HOMEDIR."funcs/credits_transfer.php"); function create_client() { global $my, $mobile, $email, $companyName, $firstName, $lastName; $clientID = 12; credits_transfer(); // using this as an example. see below for its contents } Here is the test function function credits_transfer() { global $clientID; echo $clientID } Its not because I have already called global inside the base function because I have tested without Then only thing I can think is maybe because im executing a function within a function it loses its right to use global? Quote Link to comment https://forums.phpfreaks.com/topic/164016-solved-global-not-working/#findComment-865244 Share on other sites More sharing options...
PFMaBiSmAd Posted June 29, 2009 Share Posted June 29, 2009 A) You need to stop using global to pass parameters into functions, and B) $clientID is not a global variable. It is a local variable that only exists in the create_client() function. Quote Link to comment https://forums.phpfreaks.com/topic/164016-solved-global-not-working/#findComment-865252 Share on other sites More sharing options...
dzelenika Posted June 29, 2009 Share Posted June 29, 2009 you have 2 $clientID's. One in function create_client() and other global which you echoed in credits_transfer() Quote Link to comment https://forums.phpfreaks.com/topic/164016-solved-global-not-working/#findComment-865254 Share on other sites More sharing options...
jaymc Posted June 29, 2009 Author Share Posted June 29, 2009 Here is a slimmed down example, please try <? function first() { $beans = 50; second(); } function second() { global $beans; echo $beans; echo "The function works, but did it echo 50 above?"; } first(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/164016-solved-global-not-working/#findComment-865256 Share on other sites More sharing options...
jaymc Posted June 29, 2009 Author Share Posted June 29, 2009 A) You need to stop using global to pass parameters into functions, and B) $clientID is not a global variable. It is a local variable that only exists in the create_client() function. A) Sometimes it makes its better to just use global B) But surely using global $clientID; gives the function access to its contents? Quote Link to comment https://forums.phpfreaks.com/topic/164016-solved-global-not-working/#findComment-865259 Share on other sites More sharing options...
jaymc Posted June 29, 2009 Author Share Posted June 29, 2009 B) $clientID is not a global variable. It is a local variable that only exists in the create_client() function. Yeh your correct thats the answer Cheers Quote Link to comment https://forums.phpfreaks.com/topic/164016-solved-global-not-working/#findComment-865262 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.