Yannis Berrouag
Yannis Berrouag

Reputation: 356

array with reference implicitely create key ?

can someone explain me why this code prints true without any error or warning ?

if i use the same code without the reference on the array then if will give me false and a notice on the index not existing.

what is the difference between the two that explain such a behavior, is it a bug ?

I tested this code with php 7.0

<?php
error_reporting(E_ALL);
ini_set('display_errors', true);

$bar = [];
$foo = &$bar['key'];
echo array_key_exists("key", $bar)? "true" : "false";

Upvotes: 0

Views: 88

Answers (1)

BenRoob
BenRoob

Reputation: 1722

Not a real SO answer, but:

Referring to this answer, &$bar['key'] creates the index in $bar with a value of null, because key was not present before. Now it is and true is printed.

Keep those references in mind:

Upvotes: 2

Related Questions