But halfway down the template file, I’m trying to access the $errors variable, like below, which is returning the “undefined variable” error:
if($errors['fullname']) {}
I’m relatively new to working with namespaced functions in PHP, so I have a feeling I’m missing a key step when trying to work with this variable. What am I missing?
Post your template code? Sounds like a scoping issue.
You could be setting $errors inside of a conditional statement. You could be trying to access it inside of a closure without a use declaration. You might have inadvertently unset() it somewhere.
Few ways you could be running into that problem. If you can’t figure it out, feel free to post your code so someone can actually take a look at it.
Yeah, you’re setting it inside of a conditional statement. And you should be using isset() to check for those keys (or use the aptly named array_has_key()).
To fix…
Set $errors up at the top.
$errors = [];
Use isset()
Change if($errors[...]) to if(isset($errors[...]) (where ... represents the various keys you have, such as name or whatever, I just didn’t feel like typing them all out).