-1

Possible Duplicate:
Using global vars within a function in PHP the way you do it in Javascript

For some reason, the following program is giving me an error:

<?php
    $a = 1;

    function func() {
        echo $a;
    }

    func();
?>

That is the entire program. The error is:

Notice: Undefined variable: a in what.php on line 5

Am I misunderstanding something fundamental?

Community
  • 1
  • 1
Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
  • 3
    I find it interesting that the first Google result for "global variables php" http://php.net/manual/en/language.variables.scope.php starts with an example that is remarkably similar to your code. – Russell Borogove May 30 '12 at 00:35
  • @RussellBorogove yes, that is remarkable. – Seth Carnegie May 30 '12 at 00:36
  • 1
    Bottom line: The function is written to utilize `$a` as a global variable, despite how furious the SO community is about "globals being evil". I highly doubt this is the entire logic behind the OP's application, and using a global variable solves the problem. – nickb May 30 '12 at 00:39
  • @RussellBorogove - If he's new enough to programming how would he know to add the word "global" to his query? – Galen May 30 '12 at 00:40
  • @Galen, he's got 29K rep and a bunch of 10+ answers to C/C++ programming questions under his belt. – Russell Borogove May 30 '12 at 00:41
  • 1
    @RussellBorogove C makes sense, PHP does not :) – Seth Carnegie May 30 '12 at 00:42
  • I've decided to make the variable a `static` local variable (I'm surprised to find that PHP has `static`) because it's an array and I'd like to avoid recreating it every time the function is called. – Seth Carnegie May 30 '12 at 00:44

2 Answers2

4

Due to variable scope $a is not available inside of the function. You need to pass it as an argument for func() to have access to it:

<?php
    $a = 1;

    function func($a) {
        echo $a;
    }

    func($a);
?>
John Conde
  • 217,595
  • 99
  • 455
  • 496
2

If you'd like to reference a global variable, you'll need to make use of the global keyword:

<?php
    $a = 1;

    function func() {
        global $a;

        echo $a;
    }

    func();
?>
Ken Wayne VanderLinde
  • 18,915
  • 3
  • 47
  • 72
  • 1
    Despite being recommended against, this is still correct. I won't downvote it, but I'm certain that others will. :-P – ghoti May 30 '12 at 00:40
  • Anybody that downvotes this is just regurgitating whatever "best practice" they read on the Internet and claiming to have some inherent wisdom behind the language. While there are plenty of use-cases of when **not** to use global variables, in the scope and simplicity of this question, this answer is perfectly viable. Hence, +1. – nickb May 30 '12 at 00:42
  • @ghoti: These days, using globals is recommended against in pretty much every imperative language I can think of. But they also exist in pretty much every imperative language, so there must still be legitimate uses for them. – Ken Wayne VanderLinde May 30 '12 at 00:45