php - Undefined index if the array -
i using following function options stored in wordpress.
function get_settings( $option ) { $options = get_option( 'my_options', my_default_options() ); return $options[ $option ]; } in above function my_default_options() returns array has default values. if call above function like: get_settings("title"); work fine if "title" exists in default options array.
however if title not exist in default options array, following warning:
notice: undefined index: how can fix notice? tried following:
function get_settings( $option ) { $defaults = my_default_options(); if(in_array($option, $defaults)){ $options = get_option( 'my_options', my_default_options() ); return $options[ $option ]; } } but still returns same notice.
make sure if exists or not using isset
function get_settings( $option ) { $defaults = my_default_options(); if(in_array($option, $defaults)){ $options = get_option( 'my_options', my_default_options() ); return isset($options[ $option ]) ? $options[ $option ] : ''; } }
Comments
Post a Comment