frank1002us Posted September 20, 2010 Share Posted September 20, 2010 i want to use a variable that is created inside of a function in another function some thing like this function show_category ($cats, $index, $level) { global $categories_use_categories_top_only; $categories_use_categories_top_only='no'; . . $catRow = '<tr class="scPL-' . $level . '">' . "\n" . '<th colspan="' . PL_COLCOUNT . '">' . $cats[$index]['categories_name'].'<br><i>'. $categories_use_categories_top_only . '</i><br><i>'. $cat_top . '</i></th>' . "\n" . '</tr>' . "\n"; return $catRow; } function show_product ($cats, $cat_index, $prod_index) { global $categories_use_categories_top_only; . . if ($categories_use_categories_top_only=='no' ) { include(DIR_WS_MODULES . 'options.php'); } . . i want to use $categories_use_categories_top_only which its value determined in the first function in the second function as i showed. the first function acts correctly how can i do this? Quote Link to comment https://forums.phpfreaks.com/topic/213851-use-variable-outside-of-function/ Share on other sites More sharing options...
btherl Posted September 20, 2010 Share Posted September 20, 2010 The way you're using it in the code you pasted is correct. Is it not acting how you expect it to? Quote Link to comment https://forums.phpfreaks.com/topic/213851-use-variable-outside-of-function/#findComment-1113020 Share on other sites More sharing options...
PFMaBiSmAd Posted September 20, 2010 Share Posted September 20, 2010 The variable would need to be a main program (global) variable to get what you are trying to work. However, if you have variable that has meaning to both functions so that it would normally be shared between those functions, you should be using a class, not functions. Quote Link to comment https://forums.phpfreaks.com/topic/213851-use-variable-outside-of-function/#findComment-1113029 Share on other sites More sharing options...
frank1002us Posted September 20, 2010 Author Share Posted September 20, 2010 The way you're using it in the code you pasted is correct. Is it not acting how you expect it to? no it is not, i am not getting it in the second function The variable would need to be a main program (global) variable to get what you are trying to work. However, if you have variable that has meaning to both functions so that it would normally be shared between those functions, you should be using a class, not functions. this is a gpl code that i am trying to make it work for me. i really can't write the whole thing. is there a work around? i appreciate any help you can provide Quote Link to comment https://forums.phpfreaks.com/topic/213851-use-variable-outside-of-function/#findComment-1113036 Share on other sites More sharing options...
btherl Posted September 20, 2010 Share Posted September 20, 2010 Are you expecting the variable to keep its value between script runs? Ie, you access one url and the variable is set, then you access another url and the variable is checked? Quote Link to comment https://forums.phpfreaks.com/topic/213851-use-variable-outside-of-function/#findComment-1113051 Share on other sites More sharing options...
frank1002us Posted September 20, 2010 Author Share Posted September 20, 2010 no just within the script and the value would change within the script also. it is a loop the second function is called after the first function. i want the second function to use the new value each time that it is called. Quote Link to comment https://forums.phpfreaks.com/topic/213851-use-variable-outside-of-function/#findComment-1113066 Share on other sites More sharing options...
PFMaBiSmAd Posted September 20, 2010 Share Posted September 20, 2010 Someone already posted in this thread what you would need to do - The variable would need to be a main program (global) variable to get what you are trying to work. The variable has got to be a global variable to start with for the global keyword to make it available inside of the function. Variables created inside of a function, unless they are declared as static, are all stack based and are destroyed when the function returns/ends. Quote Link to comment https://forums.phpfreaks.com/topic/213851-use-variable-outside-of-function/#findComment-1113077 Share on other sites More sharing options...
btherl Posted September 20, 2010 Share Posted September 20, 2010 PFmabismad, he is declaring it as global within both functions. So it's a global variable. I'm guessing the reason it's not working is something to do with the flow of control, but it's difficult to tell with just a program fragment. And the whole program would be quite large. Frank, perhaps you can try printing out the value of the variable at different stages in the program, also printing out something to identify what part of the program it's getting printed from. Then you can see how its value changes during execution. Quote Link to comment https://forums.phpfreaks.com/topic/213851-use-variable-outside-of-function/#findComment-1113088 Share on other sites More sharing options...
chintansshah Posted September 20, 2010 Share Posted September 20, 2010 I suggest to use inheritance. function show_product ($cats, $cat_index, $prod_index) extends show_category() So, you can use parent class values and methods in your current class. Quote Link to comment https://forums.phpfreaks.com/topic/213851-use-variable-outside-of-function/#findComment-1113091 Share on other sites More sharing options...
PFMaBiSmAd Posted September 20, 2010 Share Posted September 20, 2010 he is declaring it as global within both functions. So it's a global variable No, its not. Just try it for yourself. Unless there is a main program variable by that name, using global in a function does nothing. Quote Link to comment https://forums.phpfreaks.com/topic/213851-use-variable-outside-of-function/#findComment-1113094 Share on other sites More sharing options...
btherl Posted September 20, 2010 Share Posted September 20, 2010 @Pfmabismad: <?php function a() { global $g; $g = "I'm global\n"; } function b() { global $g; print $g; } a(); b(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/213851-use-variable-outside-of-function/#findComment-1113096 Share on other sites More sharing options...
KevinM1 Posted September 20, 2010 Share Posted September 20, 2010 I suggest to use inheritance. function show_product ($cats, $cat_index, $prod_index) extends show_category() So, you can use parent class values and methods in your current class. 1. I don't believe the original poster is writing OOP. 2. You can't extend methods. Quote Link to comment https://forums.phpfreaks.com/topic/213851-use-variable-outside-of-function/#findComment-1113197 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.