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 isse开发者_如何学Ct()
, but it still fails.
What do I need to do to avoid this warning?
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"' : '')
Change isset($imgRight)
to is_array($imgRight)
. I'm assuming that the value for the checkbox is using array notation for its value.
$imgRight
will pass isset()
if it's false.
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;
精彩评论