PHP - inconsistency with "Undefined offset" error

Why does PHP throw the error “Notification: Undefined offset” here:

<?php
$MyVar = array();
echo $MyVar[0]; //Notice: Undefined offset: 0
?>

But not here:

<?php
$MyVar = false;
echo $MyVar[0]; //No error
?>
+4
source share
2 answers

This is ultimately because in your second example $MyVar[0]it is null, which is not an error. You could probably refer to $MyVar[0][1][2][3]and get the same result.

The first example is not the zero missing index in the array, so it warns you.

0
source

An undefined offset is provided when there are no values ​​in the array and you are trying to reference an uninitialized index.

() , [0].

1- 3- , undefined.

, , . C-, malloc ( ). , PHP , undefined index [offset]

0

Source: https://habr.com/ru/post/1678654/


All Articles