in my plugin option page, i used an option checkbox. when it checked, this is ok. but when i unchecked it, there showing an error, this :
Undefined index: enable in C:xampphtdocshorrorwp-contentpluginsself-photo-galleryphoto-gallery.php on line 173
/>
the line 173 is :
<input id="spg_settings[enable]" name="spg_settings[enable]" type="checkbox" value="1" <?php checked(1, $spg_options['enable']); ?> />
but there is another fields
<input type="text" id="spg_settings[twitter_url]" name="spg_settings[twitter_url]" value="<?php echo $spg_options['twitter_url']; ?>"/>
it works fine and didn’t showing any error like this.
Add single quotes.
Use
spg_settings['enable']
insteadAlways use quotes around a string literal array index. For example, $foo[‘bar’] is correct, while $foo[bar] is not. But why? It is common to encounter this kind of syntax in old scripts:
This is wrong, but it works. The reason is that this code has an undefined constant (bar) rather than a string (‘bar’ – notice the quotes). PHP may in the future define constants which, unfortunately for such code, have the same name. It works because PHP automatically converts a bare string (an unquoted string which does not correspond to any known symbol) into a string which contains the bare string. For instance, if there is no defined constant named bar, then PHP will substitute in the string ‘bar’ and use that.
UPDATE1:
replace
with follow line of code
UPDATE2:
And replace
with
For more information check here
try this
Try to put quotes and perform
print
when using the array index values: