phpphp-7array-key-exists

PHP7: array_key_exists() - something is wrong


I have such problem in PHP 7.0.4, the same code works fine in PHP 5.6.x and older:

  function array_item(&$array,$key,$default=''){
    /* next line has number 1965 in original source */
    if(is_array($array) && array_key_exists($key,$array)) return $array[$key];
    return $default;
  }

The function simply returns a value specified by index from given array if exists, or default value if not exists. I cannot understand how it can return this message

Notice: Undefined index: TagFilter_info2_system in F:\EclipseWorkspaces\Ramses\www\RamsesLib.php on line 1965

If I replace PHP with older version then all is OK. Is possible there is so bad bug in PHP 7 or has anybody another idea? Passing array by value doesn't help. The index "TagFilter_info2_system" really not exists. If I call function array_keys($array) it returns array of indexes then doesn't contain value "TagFilter_info2_system".

Now I stripped my source codes and have got clean minimal and verifiable example:

<?php

$a=1;
if(array_key_exists("b", $GLOBALS)){
  print "Yes, \"b\" is found in array_keys(\$GLOBALS) even it is not defined yet;<br>";
  $tryToGet=$GLOBALS["b"]; // It returns error, index not found
}

print "Printing array_keys(\$GLOBALS):<br>";
print_r(array_keys($GLOBALS));

$b=1;

Solution

  • I found it is a registered bug
    https://bugs.php.net/bug.php?id=71721
    related to
    https://bugs.php.net/bug.php?id=71695

    Workaround: don't use $GLOBALS as argument, in most cases you can use isset() function