2

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;
Hink
  • 1,054
  • 1
  • 15
  • 31
  • 2
    Please give us an [mcve] so we can reproduce your problem – Rizier123 Mar 29 '16 at 09:22
  • 4
    The famous PHP 6.x which never existed? – deceze Mar 29 '16 at 09:24
  • [Looks like it works well enough](https://3v4l.org/aK3IZ) – Mark Baker Mar 29 '16 at 09:25
  • 1
    What if you try with `isset()` instead of `array_key_exists()`? – arkascha Mar 29 '16 at 09:29
  • Misclick 6.x corrected to 5.6 – Hink Mar 29 '16 at 09:32
  • 2
    Show us some sample data.... is your index enumerated, does it have an entry with key `0`? – Mark Baker Mar 29 '16 at 09:34
  • isset() works, thank you: if(is_array($array) && (array_key_exists($key,$array)) && isset($array[$key])), but the question about functionality "array_key_exists" remains – Hink Mar 29 '16 at 09:35
  • 1
    Don't use both `isset` and `array_key_exists`, use one or the other. It would still be interesting to see sample data and a concrete reproducible case that doesn't work; because this *should* work. – deceze Mar 29 '16 at 09:36
  • Back onto trees: I found $GLOBALS has been passed as the $array argument by reference. It contains 489 keys, all of string type, none is zero or numeric. I tried to reproduce but not succesfully. I hope only $GLOBALS may be affected in some circumstances. It is rare in my application and I can avoid it. The reason remains unexplained. – Hink Mar 29 '16 at 13:47
  • Minimal, veryfiable and complete example has been added. The malfunction probably concerns only $GLOBAL variable. – Hink Mar 30 '16 at 22:57

1 Answers1

3

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

Hink
  • 1,054
  • 1
  • 15
  • 31