-1

I'm not new to programming but I am very new to PHP. I just can't figure out why this variable is not being recognised. I have a file called utils.php in directory utils like this:

<?php
    $the_var = 'A'

    function foo($bar) {
        echo $bar;
    }
?>

...and another file called work.php in a parent directory of utils like this:

<?php
    include('utils/utils.php');
    function doIt() {
        echo $the_var; // is always empty
        foo('bar'); // no problem
    }
?>

Why can't the variable $the_var be accessed?

RTF
  • 6,214
  • 12
  • 64
  • 132

1 Answers1

-1

Variable inside function is not global. If you can access to variable $the_var use

function doIt($the_var) {
    echo $the_var;
    foo('bar'); // no problem
}

or

function doIt() {
    echo $GLOBALS['the_var'];
    foo('bar'); // no problem
}