ScrewLooseSalad Posted July 17, 2013 Share Posted July 17, 2013 I wrote a long explaination, but then the page was refreshed an I lost it... essentially I can't get variables to work between functions, resorted to globals, I can get away with it, but I can't even get that to worl... code: <?php global $checkvar; //get the 'checkvar' this is an array of strings $checkvar = $_GET['checkvar']; checkvariable(); function checkvariable() { var_dump($checkvar); } ?> the dump of $checkvar comes up NULL in the function, but fine right after its been called. Quote Link to comment https://forums.phpfreaks.com/topic/280245-variables-between-multiple-functions/ Share on other sites More sharing options...
mac_gyver Posted July 17, 2013 Share Posted July 17, 2013 forget that you ever saw the GLOBAL keyword. it is only used inside of a function definition. if putting GLOBAL $var; in your main code made that variable appear inside of every function, you would need to keep track of every variable name used in every function in your application to avoid reusing one of them. the correct way of getting a value (variable, string, result of another function call) into a function is to pass it in as a call time parameter when you call that function - checkvariable($checkvar); // call your function with the parameter you want to supply it with as an input function checkvariable($var) { var_dump($var); } Quote Link to comment https://forums.phpfreaks.com/topic/280245-variables-between-multiple-functions/#findComment-1441094 Share on other sites More sharing options...
davidannis Posted July 17, 2013 Share Posted July 17, 2013 Note that you can always access superglobals like $_SESSION inside a function even if the variable is not passed in. Quote Link to comment https://forums.phpfreaks.com/topic/280245-variables-between-multiple-functions/#findComment-1441111 Share on other sites More sharing options...
trq Posted July 18, 2013 Share Posted July 18, 2013 Note that you can always access superglobals like $_SESSION inside a function even if the variable is not passed in. But that is a terrible idea as it completely breaks the encapsulation provided by a function in the first place. Quote Link to comment https://forums.phpfreaks.com/topic/280245-variables-between-multiple-functions/#findComment-1441175 Share on other sites More sharing options...
ScrewLooseSalad Posted July 18, 2013 Author Share Posted July 18, 2013 Note that you can always access superglobals like $_SESSION inside a function even if the variable is not passed in. Oh yeah, I forgot about that, I might well end up using $_SESSION; But that is a terrible idea as it completely breaks the encapsulation provided by a function in the first place. It doesn't matter, I'm only using functions to break down a large task I'm having difficulty getting my head around Quote Link to comment https://forums.phpfreaks.com/topic/280245-variables-between-multiple-functions/#findComment-1441211 Share on other sites More sharing options...
mac_gyver Posted July 18, 2013 Share Posted July 18, 2013 (edited) using functions to break down a large task I'm having difficulty getting my head around that's all the more reason to NOT break encapsulation that is provided by functions. a function should only interact with the calling code at the point where the function is being called. if you read the calling code, you should be able to see what values are being supplied as inputs (the call time parameters), the function name itself indicates what the function does, and any result is returned at the point where the function is called. once you write and test your function, you should be able to forget what the actual code and variables in it are. it becomes a black box, reducing the amount of information you must actively keep track of when coding. by using global variables inside a function, you must now keep track of which functions use which variables and you can no longer tell just by looking at the calling code what inputs a function uses (a week or a month from now, this will be important because you will have forgotten exactly what you did in each function and you shouldn't need to remember exactly what you have done in each function.) this doesn't reduce the amount of information you must keep track of, it actually makes more work for you. an example using your checkvariable() function in the first post. a month from now, when you try (or someone else tries) to figure out what your code is doing, which would you rather see in your code - checkvariable(); // check what variable? i guess i'll need to find and read or remember what the checkvariable function is to find out. or checkvariable($_GET['checkvar']); // check the $_GET['checkvar'] in this call checkvariable($_POST['checkvar']); // check the $_POST['checkvar'] in this call Edited July 18, 2013 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/280245-variables-between-multiple-functions/#findComment-1441262 Share on other sites More sharing options...
davidannis Posted July 18, 2013 Share Posted July 18, 2013 But that is a terrible idea as it completely breaks the encapsulation provided by a function in the first place. I am not advocating that you use superglobals as a way to pass data in and out of functions, just pointing out that they break the rule of encapsulation. When I have a lot of values to pass out of a function my approach is often to build an array and use the array as a return value. What do you think of that approach? Quote Link to comment https://forums.phpfreaks.com/topic/280245-variables-between-multiple-functions/#findComment-1441276 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.