8

I'm curious to know if the following behaviour in PHP is intended or not. And, if it is intended, it is considered acceptable to initialize an array from a null variable by creating an index into it (as is done in the first code snippet)?

error_reporting(E_ALL);
$arr = null;

echo ($arr["blah"]===null) ? "null" : $arr["blah"];

$arr["blah"] = "somevalue";
echo "<br>";
echo ($arr["blah"]===null) ? "null" : $arr["blah"];
var_dump ($arr);

This outputs

null
somevalue

array (size=1)
   'blah' => string 'somevalue' (length=9)

However, if the array is initialized first (see code below), I get the exact same output, but an "Undefined Index" notice is given when I first try $arr["blah"]

error_reporting(E_ALL);
$arr = array();

echo ($arr["blah"]===null) ? "null" : $arr["blah"];

$arr["blah"] = "somevalue";
echo "<br>";
echo ($arr["blah"]===null) ? "null" : $arr["blah"];
var_dump ($arr);
14
  • It is intended, but highly disrecommended. It's a relic of the past, not anything that should be used in good code. Always initialize your variables before use. Commented Nov 15, 2013 at 19:34
  • 4
    You should look into the functions isset(), is_null(), and empty(). Welcome to loosely-typed, implicit almost-everything hell. Commented Nov 15, 2013 at 19:40
  • I may not be understanding this question fully, however when "assigning", one would use the = operator instead of checking as a conditional statement of == or === am I missing something here? IMO, you can't pass off a conditional as an echo Commented Nov 15, 2013 at 19:46
  • 2
    @Fred-ii- those conditional checks are just for me for debugging to print out what's going on. the question is about why one can assign a value to an index of a variable that is null. Seems like a bad idea. $arr=null; $arr["index"] = "value"; Commented Nov 15, 2013 at 19:52
  • 4
    possible duplicate of Why does PHP not complain when I treat a null value as an array like this? Commented Nov 16, 2013 at 5:56

2 Answers 2

2

PHP won't attempt the comparison if the array is null.

In the second circumstance, a comparison does occur because the array is set. PHP does not check to see if it is empty.

Your ternary is attempting to access the variable $arr["blah"], not checking to see if it is set before doing a comparison.

The proper way to write this would be:

error_reporting(E_ALL);
$arr = array();

if(isset($arr["blah"])) echo ($arr["blah"]===null) ? "null" : $arr["blah"];

$arr["blah"] = "somevalue";
echo "<br>";
if(isset($arr["blah"])) echo ($arr["blah"]===null) ? "null" : $arr["blah"];
var_dump ($arr);
Sign up to request clarification or add additional context in comments.

Comments

1

Actually, John Vargo was correct. If a variable is null, accessing it as if it were an array will simply return null without notices. This will change in the upcoming 7.4 version, then it will produce a notice.

Notice: Trying to access array offset on value of type null

The actual output is still the same.

Comments

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.