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.

3
  • 3
    You're misunderstanding variable scope in php php.net/manual/en/language.variables.scope.php Commented Sep 25, 2013 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. Commented Sep 25, 2013 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. Commented Sep 25, 2013 at 19:27

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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);

1 Comment

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).
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");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.