1

This is blowing my mind...

I've got a standalone PHP file, and a simple function with a global var.

<?php
    $var = 4;

    function echoVar()
    {
        echo $var; 
    }

    echoVar();
?>

When I call echoVar() nothing is returned... However if I place the $var inside the function it will return 4.

What's going on here? Shouldn't $var be global in this case?

Allen S
  • 3,133
  • 4
  • 27
  • 45
  • Take a look at this: http://php.net/manual/en/language.variables.scope.php – Babblo Nov 29 '13 at 13:13
  • You seem to be confusing `PHP` with how scope works in some other languages (js for example). Please [refer to the manual to see how scope works in PHP](http://php.net/manual/en/language.variables.scope.php) – Wrikken Nov 29 '13 at 13:14
  • Try like this: function echoVar() { global $var; echo $var; } – kidz Nov 29 '13 at 13:16

5 Answers5

5

If a variable is set outside of a function, it's not visible inside that function. To access it, you must declare it global with the global keyword. This is called a scope.

<?php

$var = 4;

function echoVar() {
    global $var;

    echo $var;
}

echoVar();

Note: This is generally considered bad practice. Read this for more information.

A good alternative would be to pass in the variable as an argument:

<?php

$var = 4;

function echoVar($var) {
    echo $var;
}

echoVar($var);
Community
  • 1
  • 1
Terry Harvey
  • 8,338
  • 1
  • 15
  • 22
  • Thank you this helped me understand it much better. I come from JS world so this seemed very strange indeed! – Allen S Nov 29 '13 at 13:27
3

Lots of options here... like

<?php
    $var = 4;

    function echoVar($var)
    {
        echo $var; 
    }

    echoVar($var);
?>

or

<?php
    $var = 4;

    function echoVar()
    {
        global $var;
        echo $var; 
    }

    echoVar();
?>
superphonic
  • 7,598
  • 6
  • 28
  • 61
  • Thanks.. fixed. As for bad practice, it's just another example on how to accomplish this. The code is there to be used. – superphonic Nov 29 '13 at 13:18
0

You can either have $var as an argument, like this:

$var = 4;

function echoVar($var)
{
    echo $var; 
}

echoVar($var);

or use global, like this:

$var = 4;

function echoVar()
{

    global $var;
    echo $var; 
}

echoVar();
Ana Claudia
  • 451
  • 5
  • 13
0

When you call any function it's create local variable so you have to pass argument in calling function part.

    $var = 4;

    function echoVar($var)
    {
        echo $var; 
    }

    echoVar($var);
Martin C
  • 395
  • 1
  • 7
  • 20
0

Just going to clarify since everyone seems to be posting mostly rubbish.

  • Do not use global $var;
  • Do not echo out inside of a function
  • Output from a function does not need to be assigned to a variable before being echo'd

This is how it "should" be done.

<?php
    $var = 4;  //set initial input var this is external to the function

    function echoVar($internalvar) {  /*notice were accepting $var as $internalvar I'm doing this to clarify the different variables so you don't end up getting confused with scope  $internalvar is local to the function only and not accessible externally*/
        return $internalvar; //now we pass the function internal var back out to the main code we do this with return you should never echo out your output inside the function
    }

    echo echoVar($var);  //call function and pass $var in as an arguement
?>
Dave
  • 3,184
  • 2
  • 21
  • 40