1

I have this test function in php:

funtion drop() {
    global $test_end;

    if(file_exists("test.php")) {
        $ddr="ok";
    }

    $test_end="ready";
}

I know if I call drop() it gives me "ok", as an example.

My question is this: if I define a global variable inside a function, how can I output the value of this variable inside the function, and also outside of the function when executed?

For example, calling drop(), and then running echo $test_end; outside of the function to get the value:

drop();
echo $test_end;
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 1
    I think you just have to define $test_end before calling the function! $test_end="unsure"; drop(); echo $test_end; – MatWer Oct 19 '19 at 19:19
  • 2
    [This should work.](https://3v4l.org/GIsM3) However, this looks pretty bad. Is there any reason you wouldn't just return the value from that function? – Jeto Oct 19 '19 at 19:22

2 Answers2

3

Do not use global variables, it is a bad design because it makes your code confusing and hard to read. There are much better alternatives.

Given your simple example, you can just return the value from the method:

function drop()
{
    if(file_exists("test.php"))
    {
        $ddr="ok";
    }

    $test_end="ready";
    return $test_end;
}

$test_end = drop();

If you have a more complicated case and for some reason cannot return the value, pass the variable by reference by prefixing with &:

funtion drop(&$test_end)
{
    if(file_exists("test.php"))
    {
        $ddr="ok";
    }

    $test_end="ready";
}

$test_end = null;
drop($test_end);
echo $test_end; // will now output "ready"

Passing by reference is also not a great pattern, because it still makes your code confusing.

More on why global variables are bad

The problem is if I'm looking at your code and all I see is this:

drop();
echo $test_end;

I have no idea how $test_end got set or what it's value is. Now let's say you have multiple method calls:

drop();
foo();
bar();
echo $test_end;

I now have to look at the definition of all these methods to find out what the value of $test_end is. This becomes a very big problem in larger code bases.

patrick3853
  • 1,100
  • 9
  • 17
-3

Global variables are not a bad design pattern. But having lots of global variables is usually a sign of bad programming. You should try to minimize them.

To retrieve the value, you simply reference it:

 function set()
 {
    global $test_end;
    $test_end="ready";
 }
 function show()
 {
    global $test_end;
    print "in show() value=$test_end\n";
 }
 function noscope()
 {
     print "in noscope() value=$test_end\n";
 }
 $test_end="begin";
 print "In global scope value=$test_end\n";
 show();
 noscope();
 set();
 print "after calling set()\n";
 print "In global scope value=$test_end\n";
 show();
 noscope();
symcbean
  • 47,736
  • 6
  • 59
  • 94
  • 2
    PHP has some superglobal variables, like `$_GET` and `$_POST`. It's ok to use those as *readonly* variables (unless you are using a framework that provides another way to access get and post variables). You do not need the keyword `global` to access those. Regarding the keyword `global`, I would strongly recommend to keep the number of occurrences strictly less than 1. If you need something from global scope, pass in a parameter. And if you want to change something, use a return value. That is much more predictable and allows for testable code. – Arjan Oct 19 '19 at 21:53
  • 1
    Globals are absolutely bad, and your example illustrates why. I shouldn't have to dig through every method definition in your codebase to find where a variable is getting set. When you start having nested method calls and lots of methods in your code, it became almost impossible to track down the globals. Plus, the order methods are called can now have unintended side effects, because they are changing values outside their scope. – patrick3853 Oct 19 '19 at 22:27
  • Plenty of people have already covered why globals are bad by the way. Here's just one reference: https://wordpress.stackexchange.com/a/89271 – patrick3853 Oct 19 '19 at 22:30
  • Perhaps you could explain how to instantiate objects without using global scope? Or how to implement garbage collection without relying on reachability? – symcbean Oct 20 '19 at 17:02