Jump to content


Photo

variables between multiple functions

variable php function global variable

  • Please log in to reply
6 replies to this topic

#1 ScrewLooseSalad

ScrewLooseSalad

    Advanced Member

  • Members
  • PipPipPip
  • 84 posts

Posted 17 July 2013 - 11:24 AM

  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.



#2 mac_gyver

mac_gyver

    Advanced Member

  • Moderators
  • 1,883 posts

Posted 17 July 2013 - 11:40 AM

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);
}

multi-purpose programming fool and resident naysayer [We try not be negative in replies, but telling someone what they're doing wrong, while staying politically correct, isn't always going to happen.]

#3 davidannis

davidannis

    Freak Dr.

  • Members
  • PipPipPip
  • 457 posts
  • LocationOkemos, MI USA

Posted 17 July 2013 - 01:39 PM

Note that you can always access superglobals like $_SESSION inside a function even if the variable is not passed in.



#4 trq

trq

    Advanced Member

  • Administrators
  • 30,723 posts
  • LocationSydney, Australia.

Posted 17 July 2013 - 08:49 PM

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.

http://thorpesystems.com | http://proemframework.org | http://github.com/trq

SmtpCatcher - A very simple mock sendmail useful for testing PHP mail scripts.
OPM - My Linux package manager.


#5 ScrewLooseSalad

ScrewLooseSalad

    Advanced Member

  • Members
  • PipPipPip
  • 84 posts

Posted 18 July 2013 - 04:41 AM

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



#6 mac_gyver

mac_gyver

    Advanced Member

  • Moderators
  • 1,883 posts

Posted 18 July 2013 - 10:49 AM

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, 18 July 2013 - 10:49 AM.

multi-purpose programming fool and resident naysayer [We try not be negative in replies, but telling someone what they're doing wrong, while staying politically correct, isn't always going to happen.]

#7 davidannis

davidannis

    Freak Dr.

  • Members
  • PipPipPip
  • 457 posts
  • LocationOkemos, MI USA

Posted 18 July 2013 - 12:29 PM

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?






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com