I am running the following check in PHP (in a Slim middleware) to make sure that a certain session variable exists:
public function __invoke($request, $response, $next) { if (isset($_SESSION['errors'])) { // this is line 17 // do something } $response = $next($request, $response); return $response; }
However, my error log is swamped with the following:
PHP Notice: Undefined index: errors in /our/path/app/Middleware/ValidationErrorsMiddleware.php on line 17
What can I do to have this not show as an error?
I found a solution here: PHP: Notice Undefined index even using isset but my case is different as I’m not accessing the variable prior to the check.
Advertisement
Answer
You can use array_key_exists instead of isset
if(array_key_exists('errors', $_SESSION)) { //Do something }
Here you can find the docs: https://www.php.net/manual/es/function.array-key-exists.php