-2

As long as I don't have debug on, this if statement works fine

   if (!is_array($post_options['categoryid']['wpcatid'][1])) { 
       do something
  }

but with debug on I get this notice

Notice: Undefined index: wpcatid

question is now to define the index (I presume with isset), but will this work if it's set and empty (seems it's either set or empty)?

Anyway, any advice on how to rid of this undefined index notice?

kero
  • 10,647
  • 5
  • 41
  • 51
Allen
  • 722
  • 4
  • 13
  • 31

2 Answers2

1

You're checking if $post_options['categoryid']['wpcatid'][1] is not an array, but the preceding key 'wpcatid' also doesn't exist.

So you need to combine with isset to avoid errors.

Below is an example, but the logic that you need may require a different statement:

if (isset(
        $post_options['categoryid'], 
        $post_options['categoryid']['wpcatid'], 
        $post_options['categoryid']['wpcatid'][1]
       ) && 
    !is_array($post_options['categoryid']['wpcatid'][1])
) { /* do stuff */ } 

The above checks that everything exists in sequence before checking if $post_options['categoryid']['wpcatid'][1] is not an array. If, however, it's just not set, but you still need to run your conditional logic, you'll need to amend that as applicable.

If you're writing a Wordpress plugin you can't change the error reporting level, and you definitely shouldn't resort to the error silence @ operator.

It's better you lay out explicitly under which conditions to you want to execute that particular part of your logic.


From the comments, it sounds like what you need is this:

if (
    empty($post_options['categoryid']) || 
    empty($post_options['categoryid']['wpcatid']) ||
    empty($post_options['categoryid']['wpcatid'][1]) ||
    !is_array($post_options['categoryid']['wpcatid'][1]) 
)

In plain English:

If (there's no category id in post options... or there is, but there's no wpcatid, or there's a wpcatid, but there's no element with a key of 1 in there, or there is, but that element is not an array)

bcmcfc
  • 25,966
  • 29
  • 109
  • 181
  • that makes sense...however, since the isset is false, and the !is_array is true, this will resolve to false. I need to do stuff when this resolves to true (there is no array)..that seems to be the main problem...does this make sense? – Allen May 26 '14 at 15:54
  • @Allen it does. It's just about adjusting the checks to what you need. Added some detail. I've used empty instead of isset there - the plain English explanation underneath explains what it does. More detail here: http://stackoverflow.com/questions/1219542/in-where-shall-i-use-isset-and-empty?lq=1 – bcmcfc May 26 '14 at 15:59
  • Actually, I tried using your earlier code, which makes sense if I first check if something isset and is an array (which is what I don't want), thus suggesting this: if (!(isset( $post_options['categoryid'], $post_options['categoryid']['wpcatid'], $post_options['categoryid']['wpcatid'][1] ) && is_array($post_options['categoryid']['wpcatid'][1]) )) { //do stuff} – Allen May 26 '14 at 16:06
0

Why not just use isset()? The isset() function in PHP determines whether a variable is set and is not NULL. It returns a Boolean value, that is, if the variable is set it will return true and if the variable value is null it will return false.

Another option is to use error_reporting(E_ALL ^ E_NOTICE); You can also turn off error reporting in your php.ini file or .htaccess file, but it is not considered as a wise move if you are still in the testing stage.

Thirdly, though discoraged, is to use the @ suppress error option. You can find more information on this here. http://php.net/manual/en/language.operators.errorcontrol.php. I think you just have to stick with the first option that is to use isset()

Ali Gajani
  • 14,762
  • 12
  • 59
  • 100
  • thanks..well, this is wordpress plugin and some people, unfortunately, have debugging on, so I test with debug on to eliminate errors and notices. So would it be if (isset(x) && !is_array(x))...seems the isset would be false, the is_array true, so the result would be false - where my original statement would be true – Allen May 26 '14 at 15:28