I have a function within my PHP project which has some specific params..
$user_sequence = $user['user_sequence'];
if ($cached || !$user_sequence) {
return;
}
Notice: Undefined index: user_sequence
I have fixed that with:
if (isset($user['user_sequence'])) {
$user_sequence = $user['user_sequence'];
}
But now my second if() clause get's a warning:
Variable '$user_sequence' is probably undefined
Would setting it to null before if() be the best approach to solve it? Thanks
Spelled out in full, you need to specify what the value should be if
$user['user_sequence']is not set. In your second if statement, you are checking!$user_sequence, so a logical default value would be booleanfalse:Luckily, PHP doesn't make use write out that whole
ifstatement, because we can use the null coalescing operator, and shorten it to this:In other situations, you might want to default to an empty array (
$something ?? []) or even justnull($something ?? null).