I found out that isset($array)
not required when checking for a specific key, however I also know that there are some reasons for checking without a known key if $array
is created.
For instance:
foreach ($foo as $bar) { echo $bar; }
PHP Note: Undefined variable: foo
PHP Warning: invalid argument provided by foreach ()
it's better:
if (isset($foo)) { foreach ($foo as $bar) { echo $bar; } }
Since I use arrays a lot when working with data, and I wanted to ask if there are other cases when I have to check if the entire isset()
array exists? Or should I just check every $array[$key]
that I will use when it is known? This is related to the question of whether there are any advantages or disadvantages in doing so:
if (isset($foo[0])) { foreach ($foo as $bar) {
instead of this:
if (isset($foo)) { foreach ($foo as $bar) {
So, should I ever use isset($array)
instead of isset($array[$key])
if $key
is known?
source share