1

i know my first element in my array[zero index] is NULL and i want to check that in my {if} condition. but i have a notice here. what can i do to resolve the notice? my simple code:

$arr = [];
$arr[1] = '1';
if($arr[0] === NULL)
    echo 'if';
else
    echo 'else';
1
  • you first sentense "my first element in my array[zero index] is NULL" is not 100% correct: It's not defined, which is something different to being NULL! Commented Nov 23, 2018 at 21:09

1 Answer 1

1

What you can do is check if the variable relative to that index of the array has been initialized/set. For example:

$arr = [];
$arr[1] = '1';
if(!isset($arr[0]))
    echo 'if';
else
    echo 'else';
Sign up to request clarification or add additional context in comments.

1 Comment

Careful with isset(), because it will return false if $arr[0] is set to a falsey value (like null). It would probably be better to use array_key_exists().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.