Jump to content

variables between multiple functions


ScrewLooseSalad

Recommended Posts

  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.

Link to comment
Share on other sites

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);
}
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 by mac_gyver
Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.