Lucas
Lucas

Reputation: 3725

in_array function fails

I'm using this line:

'.((isset($imgRight) && in_array(1, $imgRight)) ? 'checked="checked"' : '').'

and, in some cases, $imgRight can be false. That's why there's isset(), but it still fails.

What do I need to do to avoid this warning?

Upvotes: 1

Views: 912

Answers (4)

Anonymous
Anonymous

Reputation: 3689

First, don't use short tags, it'll be deprecated soon! But to your question: You need to check $imgRight for its state not for its existance (isset)! You can also shortcut some of your code here...and you should also check for it's existance though before you work with it:

if (isset($imgRight)) {
   if ($imgRight != false && in_array(1, $imgRight)) { 
      $chk =  'checked="checked"' } 
   else { $chk = '';}
}

and in your checkbox element just do

echo $chk;

Upvotes: 0

Yzmir Ramirez
Yzmir Ramirez

Reputation: 1281

Change isset($imgRight) to is_array($imgRight). I'm assuming that the value for the checkbox is using array notation for its value.

Upvotes: 1

Jessedc
Jessedc

Reputation: 12470

$imgRight will pass isset() if it's false.

Upvotes: 0

timdev
timdev

Reputation: 62924

Just because something is false doesn't mean it's not set:

$foo = false;
isset($foo); //true

You can just use:

($imgRight && in_array(1, $imgRight)) ? 'checked="checked"' : '')

or to be very safe (if imgRgiht might be null, or some non-falsey value that isn't an array):

((!empty($imgRight) && is_array($imgRight) && in_array(1, $imgRight)) ? 'checked="checked"' : '')

Upvotes: 3

Related Questions