I’m getting a PHP Notice if I set a $_SESSION index without submission.
Wanted to know if there is a workaround?
The Notice:
PHP Notice: Undefined index: bar
PHP
if($validation_condition) {
$_SESSION['bar'] = 'foo';
}
The validation process stops the form from submitting, saves the form data to a database and reloads the page. The $_SESSION[‘bar’] displays the correct value but I get a Notice in the logs. Wanting to stop the Notice.
Here is the var_dump()
array(1) {
["bar"]=>
string(3) "foo"
}
as you can see it’s getting set.
There is one small difference from my original question, I’m setting it like this
$_SESSION['bar'] .= 'foo';
With the .(dot) concatenation. When I removed it the Notice went away.
SLAP HEAD ON DESK!
one approach is to ensure that
$_SESSION['bar']is set under both conditions:Alternatively, in your display code, you can first check for the existence of
$_SESSION['bar']: