0

get_population($row['ID']) is causing error Variable undefined:

foreach($filterList as $row)
{
   echo '<tr>';
   echo '<td>' . $row['county_ID'] . '</td>';
   echo '<td>' . get_population($row['ID']) . '</td>'; 
   echo '<td>' . $row['ID'] . '</td>';     
   echo '</tr>';
}

When I use a database value in the function parameter I get the error :Undefined variable: It must be out of scope but I don't know how to get around it.

If I print $row['ID'] it shows the value of 1 for this example. If I hard code 1 as the parameter value it works. It is only when I try to pass $row['ID'] to the function that I get the error.

I am sure it is a simple answer but I am chasing my tail. Thanks

8
  • 3
    Can we see your query? Perhaps the table structure? The get_population() function? Commented May 29, 2014 at 19:38
  • Might be you're getting the error inside the get_population() function? Can you post that too? Commented May 29, 2014 at 19:38
  • Are you missing $row in front of ['ID']? Commented May 29, 2014 at 19:42
  • Please post the whole error message. Undefined variable warnings contain the name of the variable that's undefined. Commented May 29, 2014 at 19:43
  • The error message also has a line number. Please point out the specific line that's getting the error. Commented May 29, 2014 at 19:44

2 Answers 2

1

In your code there's a mismatch in ''.['ID'].'';

The correct is $row['ID']:

foreach($filterList as $row)
{
   echo '<tr>';
   echo '<td>' . $row['county_ID'] . '</td>';
   echo '<td>' . get_population($row['ID']) . '</td>'; 
   // error here
   echo '<td>' . ['ID'] . '</td>';     
   // change with
   echo '<td>' . $row['ID'] . '</td>';     
   echo '</tr>';
}
Sign up to request clarification or add additional context in comments.

1 Comment

I suspect that's a copying error, because that would cause a parse error, not undefined variable.
0

The code is correct, the problem was a null value in the database. Since there was no value to pass to the function it returned variable undefined.

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.