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.