1

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?

0

4 Answers 4

3

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"' : '')
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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

Comments

0

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;

2 Comments

Try searching for short tags (not shortcode, sorry for that) and deprecated and you'll see. I know that it's not really 100% true as it was just an idea of the dev team and they decided not to do it...for now! But it might happen one day...if you do some further research, you'll also find that there are few other reasons not to use them - see e.g. here
Ah yes, there has been talk about that. I thought you were referring to the ternary operator, since there were no short tags in the OP's question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.