sorenchr Posted May 21, 2008 Share Posted May 21, 2008 Hi there Im having a troubled time wrapping my head around this problem, when i echo $testvar at the bottom line, it leaves me blank in the explorer. function setVar($var, $testvar, $changeto) { if($var == '1') { $testvar = $changeto; } elseif($var == '0') { $testvar = '0'; } else { echo "Error."; exit; } } setVar(1, $admin, norights); echo $admin; When i echo the $testvar inside the if($var == '1'), it prints it fine. I've also tried globalizing $testvar inside the if($var == '1') brackets, that doesn't seem to help either. Can anybody help me with this? Link to comment https://forums.phpfreaks.com/topic/106577-calling-variables-defined-inside-a-function/ Share on other sites More sharing options...
Mr. Despair Posted May 21, 2008 Share Posted May 21, 2008 Maybe it would be better if you return the variable. Might be better to set the match as a number instead of a string. Echoing the $admin variable won't do anything because its empty. You could store the function result in a variable and echo the variable instead. Link to comment https://forums.phpfreaks.com/topic/106577-calling-variables-defined-inside-a-function/#findComment-546309 Share on other sites More sharing options...
PFMaBiSmAd Posted May 21, 2008 Share Posted May 21, 2008 The purpose of functions are to accept some input (the parameters passed when the function is called, if any), perform some function, and return (or output) the results. Variables that are used inside of a function are only available in that function (as soon as the function ends, those variables are destroyed.) This is referred to as variable scope. If you want a variable that exists inside of a function to be available later, than you need to be using an OOP class. Link to comment https://forums.phpfreaks.com/topic/106577-calling-variables-defined-inside-a-function/#findComment-546432 Share on other sites More sharing options...
BlueSkyIS Posted May 21, 2008 Share Posted May 21, 2008 you can define variables as global inside a function, and that will cause the function to use the variable outside of it's scope while providing access to same variable to others outside of the function. example: $something = 21; function do_something() global $something; $something++; } do_something(); echo $something; output: 22 Link to comment https://forums.phpfreaks.com/topic/106577-calling-variables-defined-inside-a-function/#findComment-546441 Share on other sites More sharing options...
DyslexicDog Posted May 21, 2008 Share Posted May 21, 2008 you can define variables as global inside a function, and that will cause the function to use the variable outside of it's scope while providing access to same variable to others outside of the function. example: $something = 21; function do_something() global $something; $something++; } do_something(); echo $something; output: 22 Globals are going away in php6 this should not be a method that anyone is learning anymore. Link to comment https://forums.phpfreaks.com/topic/106577-calling-variables-defined-inside-a-function/#findComment-546453 Share on other sites More sharing options...
PFMaBiSmAd Posted May 21, 2008 Share Posted May 21, 2008 Register_globals are going away in php6. That is not the same as using the global keyword. Writing functions that rely on the global keyword to pass values is poor programming. Consider the case of all the php built-in functions. None of them require you to setup a specifically named variable in your main code so that the function can use it. If you are writing functions that need to use the global keyword to work, either you should be using OOP classes, because you have related variables and functions that belong together or you should not be using a function and just copy/paste your code where it is needed. Link to comment https://forums.phpfreaks.com/topic/106577-calling-variables-defined-inside-a-function/#findComment-546491 Share on other sites More sharing options...
BlueSkyIS Posted May 21, 2008 Share Posted May 21, 2008 mmmm, baloney Link to comment https://forums.phpfreaks.com/topic/106577-calling-variables-defined-inside-a-function/#findComment-546492 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.