1

php 7.0.8 on ubuntu 16.0.4 LTS could somebody give me a hint why an array key check would fail when the key clearly exists, program execution just stops, with no explanation even with all error reporting enabled.

The data type held in the array element is a string containing a torrent file downloaded from fedora website https://torrents.fedoraproject.org/

other functions fail here aswell such as !empty on the array key

the key in this instance is 0

if(array_key_exists($index, $this->_webpage)){
            return $this->_webpage[$index];
        }else{
            /* throw notice */
            trigger_error("Array index is out of range. Can not get webpage.", E_USER_NOTICE);
            return FALSE;
        }

NOTE: iv been using this same function to check that webpages have been downloaded and it worked on the same system, im just adding torrent parsing to my application

  • 1. Is $this->webpage array or it goes through some getters? 2. And did you try type 0 or other keys to make sure that problem is not in $index? – Dmytrechko Sep 22 '16 at 09:03
  • the content of the array is the raw content from one of the torrents from https://torrents.fedoraproject.org with a key of 0 – David Sierowski Sep 22 '16 at 09:27
  • Please run `var_dump($this->_webpage)`, just before the code in your post, and post the results here. – ChristianF Sep 22 '16 at 09:56
  • im using netbeans ide with xdebug, iv just put an echo after "if(array_key_exists($index, $this->_webpage)){" with all debugging disabled and it works. For some reason all debugging stops at that point? – David Sierowski Sep 23 '16 at 09:38

1 Answers1

0

SUMMARY array_key_exists will definitely tell you if a key exists in an array, whereas isset will only return true if the key/variable exists and is not null and empty return true if the variable is an empty string, false, array(), NULL, “0?, 0, and an unset variable

More details

isset()

From PHP manual – isset():

isset — Determine if a variable is set and is not NULL

In other words, it returns true only when the variable is not null.

empty()

From PHP Manual – empty():

empty — Determine whether a variable is empty

In other words, it will return true if the variable is an empty string, false, array(), NULL, “0?, 0, and an unset variable.

Also empty() does not generate a warning if the variable does not exist.

array_key_exists

From PHP Manual – array_key_exists():

Checks if the given key or index exists in the array

So array_key_exists() returns TRUE if the given key is set in the array. key can be any value possible for an array index.

Dmytrechko
  • 598
  • 3
  • 11