0

I'm pretty new to php and I've read many times "avoid global variables" but I've not understood whether I have to follow this advice everytime or not.

I need some values (they are stored into two variables) into a function and I could put both variables as parameters:

$GVar1 = array(...); //This is a multidimensional array (over 80.000 chars in declaration)
$GVar2 = array(...);

$Result = DoMyStuff('BasicValue', $GVar1, $GVar2);

function DoMyStuff ($BasicParameter, $GV1, GV2){
    //Do stuff
}

or declare variables as global into the function:

$GVar1 = array(...);
$GVar2 = array(...);

$Result = DoMyStuff('BasicValue');

function DoMyStuff ($BasicParameter){
    global $GVar1;
    global $GVar2;
    //Do stuff
}

I would like to understand if the first way is really better .. and why?

genespos
  • 3,211
  • 6
  • 38
  • 70
  • 3
    Have you done any research before asking? This is a very broad question that's been addressed *many* times, and isn't at all specific to PHP. – Carcigenicate Mar 06 '18 at 16:07
  • @Carcigenicate I've searched but I haven't find anything peculiar. A link could be useful ;) – genespos Mar 06 '18 at 16:07
  • 2
    Might be worth looking up [Dependency Injection](https://stackoverflow.com/questions/130794/what-is-dependency-injection) ... – CD001 Mar 06 '18 at 16:09
  • 1
    Yes the first way is better. Among other things, it encapsulates all of its requirements into the function definition. Think about somebody else reading your code. In the first case, they will clearly understand what they have to pass in. In the second case, they don't necessarily have any idea that they need to pass a parameter *and* set up some global variables with a particular name. – Alex Howansky Mar 06 '18 at 16:11
  • @CD001 Thanks! Very useful link. I'm starting to understand (I'm a self-taught) – genespos Mar 06 '18 at 16:13
  • @AlexHowansky It's better even if I need to send a very big array? I mean over 80.000 chars (It's big for me; I don't know if is it the same for a server) – genespos Mar 06 '18 at 16:27
  • And now ponder about ... `$callback = function($BasicParameter) use ($GVar1, $GVar2) { };` ... :) – IncredibleHat Mar 06 '18 at 16:29
  • @IncredibleHat Sorry, I didn't understand. I need more info about your code. Remember: I'm not a pro – genespos Mar 06 '18 at 16:31
  • Just a different way of passing a seemingly global variable into a function. Making code even more confusing to someone who comes along later ;) https://stackoverflow.com/questions/1065188/in-php-5-3-0-what-is-the-function-use-identifier – IncredibleHat Mar 06 '18 at 16:32
  • @genespos IncredibleHat's example there is an [anonymous function](http://php.net/manual/en/functions.anonymous.php) ... useful in certain cases but probably not something you'll use regularly, especially when you're just starting out in PHP... – CD001 Mar 06 '18 at 16:38
  • I would say parameter should be pass by reference. because if you update `$GVar1` and `$GVar2` values inside the function it will not updated the outside arrays. you may need to use somewhere else these arrays. – Zia Ur Rehman Mar 06 '18 at 16:47

0 Answers0