Login   Register  
PHP Classes
elePHPant
Icontem

Notice: Undefined index:

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us

      Top level forums  >  PHP Specialists  >  General  >  Notice: Undefined index:  
Subject:Notice: Undefined index:
Summary:Does this matter re: code execution time?
Messages:1
Author:Ted Rader
Date:2012-12-20 00:39:11
Update:2012-12-20 01:13:28
 

  1. Notice: Undefined index:   Reply  
Picture of Ted Rader
Ted Rader
2012-12-20 01:13:28
I'm using WordPress, and I get this in a custom plugin I created. I understand why this occurs, my question is "does it matter?"

Example:
$arr = array(
'a' => 'abc',
'b' => 'xyz'
)

if ($arr['c'] == 'whatever') {
// do something
}

Since $arr['c'] doesn't exist, a notice will appear if WordPress's debug is set to true.

Assuming WordPress debug is set to false (and notice will not appear), is it better to use:

if (array_key_exists('c', $arr) && $arr['c'] == 'whatever') {
// do something
}

What are the negatives of NOT checking if the key exists? Which way is recommended?