I have a bunch of HTML and PHP code and in the template file it works fine but I’m trying to put it in a PHP function and now when I run the page I get the error Undefined variable: variableName
Here’s some code:
function testFunction()
{
foreach ($variableName as $variable):
echo 'tasf';
endforeach;
}
Inside that function $variableName cannot be found but if I move it outside the function it can be found just fine. I’m doing this within a symfony php template file if it matters.
Simple issue of variable scope. If that variable is defined outside the function then either you need to pass it there or declare it global
See Manual Here