1

Not used PHP for quite a while, jumped back in and was hit by an error that doesn't make sense to me, this code:

$errorCount = 0;
$errorList = array();

function getParam($paramId){
    if (isset($_GET[$paramId])){
        $id = $_GET[$paramId];
    } else {
         $errorList[] = (string)$paramId;
         $errorCount++;
    };
};


getParam("id");

The error that pops up is: Undefined variable: errorCount

I can't see why that would fail, but $errorList doesn't - I'm sure it's something silly.

Richard
  • 1,110
  • 3
  • 16
  • 33
  • 3
    You're misunderstanding variable scope in php http://php.net/manual/en/language.variables.scope.php – Mike B Sep 25 '13 at 19:26
  • 1
    `$errorCount` and `$errorList` are both undefined inside `getParam`. `$errorList[]` automatically creates the variable (and makes it an array) if it doesn't exist. That's why `$errorList` "works" and `$errorCount` doesn't. – Rocket Hazmat Sep 25 '13 at 19:26
  • This is what happens when you jump off JS to PHP - I assumed global was automatically set by being outside the function. – Richard Sep 25 '13 at 19:27

3 Answers3

2

This is a variable scope issue. The variable isn't available inside the function's scope, so it'll just display the error message.

Consider the following case:

$hello = 'hello';
function test() {
    echo $hello;
}
test();

See it live!

The variable $hello is defined in the code, but when you try to execute the above code, you'll get an error saying Undefined variable: hello.

If you want your variables to be accessible inside the function, pass them as parameters, like so:

$hello = 'hello';
function test($hello) {
    echo $hello;
}
test($hello);

See it live!


Now, to fix your actual issue, you can pass $errorCount as reference:

$errorCount = 0;
$errorList = array();

function getParam($paramId, & $errorCount){
    if (isset($_GET[$paramId])){
        $id = $_GET[$paramId];
    } else {
         $errorList[] = (string)$paramId;
         $errorCount++;
    };
};

An alternative solution would be to use global variables, but this isn't a very good practice, in my opinion, and should be avoided if possible. You may want to check this post to understand why.

Refer to the PHP Manual for more information regarding this.

Community
  • 1
  • 1
Amal Murali
  • 70,371
  • 17
  • 120
  • 139
0

That is because your variables are not defined in the scope of your function. You have a few options. 1 option is to declare those variables global. Or you can create a class.

function getParam($paramId){
  global $errorList;
  global $errorCount;
    if (isset($_GET[$paramId])){
        $id = $_GET[$paramId];
    } else {
         $errorList[] = (string)$paramId;
         $errorCount++;
    };
};

You can also pass the variables in by reference:

For example:

$my_variable = array();
$my_variable_ct = 0;


function fn($arg1, $my_var, $my_var_count) {
   // do everything here
}

fn("Hello", &$my_variable, &$my_variable_ct);
Ryan McCullagh
  • 13,581
  • 8
  • 56
  • 101
  • 3
    It can also be written as `global $errorList, $errorCount`. Note that `global` variables are not always seen in a good light with most programmers (but it doesn't matter much as PHP runs very short sessions). – h2ooooooo Sep 25 '13 at 19:26
0

Normal variables that are defined outside the functions, are not accessible by default. To reach them, we should simply use global $var1, $var2; to access their value like this:

$errorCount = 0;
$errorList = array();

function getParam($paramId){

    global $errorList, $errorCount;

    if (isset($_GET[$paramId])){
        $id = $_GET[$paramId];
    } else {
         $errorList[] = (string)$paramId;
         $errorCount++;
    };
};


getParam("id");
Mohammad Naji
  • 4,964
  • 10
  • 46
  • 77