493 posts
  • Has referred 10+ members
  • Has sold $40,000+ on Envato Market
  • Has collected 50+ items on Envato Market
  • Sells items exclusively on Envato Market
+1 more
wopethemes says
Hi,
I did not care any notices from php in a long times as a php developer . but themeforest require so I must find a simple way to solve the notices.
My suggest is build a function that will not echo “un-isset” var. something like this
function prevent_notice($var){
    if(isset($var)){
        echo $var;
    }
}
so you can ignore any notices from any un-isset var without set init them.

The issue is if I want to prevent notice of an var like $user[‘name’] , then php will notice that the offset ‘name’ must created!

So anyone can know how to know array and key of $var when we use an var like $user[‘name’] inside an function.

Thanks!

3538 posts Ruben Bristian
  • Sells items exclusively on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
  • Has sold $500,000+ on Envato Market
  • Has been part of the Envato Community for over 6 years
+10 more
KrownThemes says

LOL!!! :D

Notices are there for you to kill them, not to hide them. Just fix the errors! :D

492 posts
  • Has referred 1000+ members
  • Has sold $250,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+8 more
pixelentity says

function prevent_notice($var){
    if(isset($var)){
        echo $var;
    }
}
inefficient due the extra function call.

BF

493 posts
  • Has referred 10+ members
  • Has sold $40,000+ on Envato Market
  • Has collected 50+ items on Envato Market
  • Sells items exclusively on Envato Market
+1 more
wopethemes says


function prevent_notice($var){
    if(isset($var)){
        echo $var;
    }
}
inefficient due the extra function call. BF
why not? I just call them in theme options. which only use once. it’s simple the code.
normal way (which I know)
- set init data for var
- echo var
better way
- declare prevent_notice function
- call function
so we can ignore a large init vars code lines.
let me know better way?
493 posts
  • Has referred 10+ members
  • Has sold $40,000+ on Envato Market
  • Has collected 50+ items on Envato Market
  • Sells items exclusively on Envato Market
+1 more
wopethemes says

LOL!!! :D Notices are there for you to kill them, not to hide them. Just fix the errors! :D

no I did not try to hide them. I just checked to see if it’s empty then hide it instead echo it.

493 posts
  • Has referred 10+ members
  • Has sold $40,000+ on Envato Market
  • Has collected 50+ items on Envato Market
  • Sells items exclusively on Envato Market
+1 more
wopethemes says
I figure it out myself. share here if you’re interest :D . to prevent themeforest’s notices :P
//prevent notice when the var is not set
function prevent_notice($var,$key = ""){
    if($key){
        if(isset($var[$key]) and array_key_exists($key,$var)){
            echo $var[$key];
        }
    }else{
        if(isset($var)){
            echo $var;
        }
    }
}
examples single var
//normal
echo $main_option['logo_url'];
//use function
prevent_notice($main_option['logo_url']);
example var from array
//normal
echo $checkbox_responsive[1];
//function
prevent_notice($checkbox_responsive,1)
the profit of this function that you will never need to init any data before echo it. which is very popular and a big problem in theme options. I wanna to keep my code clear. no more waste code lines for init vars. :D
3538 posts Ruben Bristian
  • Sells items exclusively on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
  • Has sold $500,000+ on Envato Market
  • Has been part of the Envato Community for over 6 years
+10 more
KrownThemes says

//normal
echo $main_option['logo_url'];
//use function
prevent_notice($main_option['logo_url']);

Couldn’t you just do:

echo isset($main_option['logo_url']) ? $main_option['logo_url'] : 'default_logo_here';

Because if you are giving the option of a logo here, you should definitely provide an alternative. It isn’t a good practice to leave the user without a logo if he doesn’t set it.. This should be the case for all theme options.

492 posts
  • Has referred 1000+ members
  • Has sold $250,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+8 more
pixelentity says

why not? I just call them in theme options. which only use once. it’s simple the code.
done few times the overhead is negligible so it really doesn’t matter but, if it’s all around in your code, including nested loops, you may want to avoid unnecessary functions call and use directly
if (isset($var)) echo $var;
493 posts
  • Has referred 10+ members
  • Has sold $40,000+ on Envato Market
  • Has collected 50+ items on Envato Market
  • Sells items exclusively on Envato Market
+1 more
wopethemes says


//normal
echo $main_option['logo_url'];
//use function
prevent_notice($main_option['logo_url']);

Couldn’t you just do:

echo isset($main_option['logo_url']) ? $main_option['logo_url'] : 'default_logo_here';
Because if you are giving the option of a logo here, you should definitely provide an alternative. It isn’t a good practice to leave the user without a logo if he doesn’t set it.. This should be the case for all theme options.
thanks for your code. if the option alway have real init so I never care for notice problem :) . because I’m alway set init for it , so the notice’s never come. I just don’t happy with init value for empty var :)
$var = ""; 
492 posts
  • Has referred 1000+ members
  • Has sold $250,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+8 more
pixelentity says

Honestly, for theme options, we just set default values once. A very simple way to do it by using http://codex.wordpress.org/Function_Reference/shortcode_atts

$options = shortcode_adds($defaults,$options)
by
by
by
by
by
by