mercredi 7 juillet 2021

PHP : I don't Understand, why this is happening? [duplicate]

Hi I've tried below php code today, I felt something strange, can someone explain this, how and why this is happening ?

$a = ['a' => 'test', 'b' => 'test1'];

if ($a['c'] !== '') {
    echo 'if block';
} else {
    echo 'else block';
}

The above code is executed and prints output as below with Notice error

PHP Notice: Undefined index: c in /tmp/a.php on line 4 if block

and

$a = ['a' => 'test', 'b' => 'test1'];

if ($a['c'] !='') {
    echo 'if block';
} else {
    echo 'else block';
}

The above code also executed and prints output as below with Notice error

PHP Notice: Undefined index: c in /tmp/a.php on line 4 else block

I know the difference between === and == and also we can use isset to fix this, but how it can be done? I'm so excited about this answer.

Aucun commentaire:

Enregistrer un commentaire