admin管理员组文章数量:1130349
$options = get_option('analytics');
if ( ! preg_match( '/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/', $options['analytics_startdate'] ) ) {
$options['analytics_startdate'] = '2018-12-01';
}
Why this is throwing an error :
Undefined index: analytics_startdate ,
though i am specifying it.
$options = get_option('analytics');
if ( ! preg_match( '/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/', $options['analytics_startdate'] ) ) {
$options['analytics_startdate'] = '2018-12-01';
}
Why this is throwing an error :
Undefined index: analytics_startdate ,
though i am specifying it.
Share Improve this question edited Nov 4, 2018 at 13:56 butlerblog 5,1413 gold badges28 silver badges44 bronze badges asked Nov 3, 2018 at 19:11 user145078user145078 6 | Show 1 more comment1 Answer
Reset to default 0though i am specifying it.
You're not actually specifying it. You're trying to use it in your regex and THEN you specified it.
You can't use it in your "if" criteria when if it doesn't exist. You need to check to see if it exists first.
The following would set your value if it is not set OR if it IS set and doesn't match your regex:
$options = get_option( 'analytics' );
if ( ! isset( $options['analytics_startdate'] ) || ! preg_match( '/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/', $options['analytics_startdate'] ) ) {
$options['analytics_startdate'] = '2018-12-01';
}
$options = get_option('analytics');
if ( ! preg_match( '/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/', $options['analytics_startdate'] ) ) {
$options['analytics_startdate'] = '2018-12-01';
}
Why this is throwing an error :
Undefined index: analytics_startdate ,
though i am specifying it.
$options = get_option('analytics');
if ( ! preg_match( '/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/', $options['analytics_startdate'] ) ) {
$options['analytics_startdate'] = '2018-12-01';
}
Why this is throwing an error :
Undefined index: analytics_startdate ,
though i am specifying it.
Share Improve this question edited Nov 4, 2018 at 13:56 butlerblog 5,1413 gold badges28 silver badges44 bronze badges asked Nov 3, 2018 at 19:11 user145078user145078 6-
does
$optionsalready containanalytics_startdatebefore you set it here? – Milo Commented Nov 3, 2018 at 19:19 - @Milo hi, No it was all empty , i added this only here in the above code..it is displaying the 2018-12-01 but along with this error..not sure why – user145078 Commented Nov 3, 2018 at 19:22
-
2
because you try to use it in your
ifcondition withpreg_matchbefore it exists. – Milo Commented Nov 3, 2018 at 19:25 - @Milo oh yeah thank you very much ..i am confused with how to save this option . also want to make sure that user saved value is asper format – user145078 Commented Nov 3, 2018 at 19:40
-
if (!isset($options['analytics_startdate']){ $options['analytics_startdate'] = '2018-12-01'; } elseif (isset($options['analytics_startdate'])){ if (!preg_match('/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/', $options['analytics_startdate'])) { $options['analytics_startdate'] = '2018-12-01'; }}will this be okay? – user145078 Commented Nov 3, 2018 at 19:42
1 Answer
Reset to default 0though i am specifying it.
You're not actually specifying it. You're trying to use it in your regex and THEN you specified it.
You can't use it in your "if" criteria when if it doesn't exist. You need to check to see if it exists first.
The following would set your value if it is not set OR if it IS set and doesn't match your regex:
$options = get_option( 'analytics' );
if ( ! isset( $options['analytics_startdate'] ) || ! preg_match( '/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/', $options['analytics_startdate'] ) ) {
$options['analytics_startdate'] = '2018-12-01';
}
本文标签: errorsUndefined index when saved to options
版权声明:本文标题:errors - Undefined index when saved to options 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749206621a2332679.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


$optionsalready containanalytics_startdatebefore you set it here? – Milo Commented Nov 3, 2018 at 19:19ifcondition withpreg_matchbefore it exists. – Milo Commented Nov 3, 2018 at 19:25if (!isset($options['analytics_startdate']){ $options['analytics_startdate'] = '2018-12-01'; } elseif (isset($options['analytics_startdate'])){ if (!preg_match('/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/', $options['analytics_startdate'])) { $options['analytics_startdate'] = '2018-12-01'; }}will this be okay? – user145078 Commented Nov 3, 2018 at 19:42