Why does PHP throw a "Notice: Undefined offset" error here:
<?php
$MyVar = array();
echo $MyVar[0]; //Notice: Undefined offset: 0
?>
But not here:
<?php
$MyVar = false;
echo $MyVar[0]; //No error
?>
It's ultimately because in your 2nd example $MyVar[0] is null which isn't an error. You could probably reference $MyVar[0][1][2][3] and get the same result.
The first example isn't null it's a missing index in an array so it warns you.
Undefined offset is provided when there are no values in an array and you're trying to reference an uninitialised index.
In case of the 2nd code, you've assigned(initialised) a value to the variable which is not different from variable[0].
If you assign values for 1st two indexes in 1st example, and refer to the 3rd index, you should get the undefined offset error.
I dont think it is about null or not, but a case of assigning values to indexes. If you take a C parlance, array is an equivalent of malloc(upto a certain extent). Thankfully PHP does not crash, just throws an undefined index[offset] error
NULL. The same offset notice applies to strings$MyVar = ''; echo $MyVar[0];Which would result inUninitialized string offset: 0Accessing variables of other types (not including arrays or objects implementing the appropriate interfaces) using [] or {} silently returns NULL.This implies that PHP expects you to verify the variable type prior to accessing it with[]or{}e.g.if (is_array($MyVar))