490 posts
  • Sold between 10 000 and 50 000 dollars
  • Referred between 10 and 49 users
  • Bought between 50 and 99 items
  • Exclusive Author
  • Has been a member for 3-4 years
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!

3439 posts Ruben Bristian
  • Most Wanted Bounty Winner
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Has been a member for 6-7 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Won a Competition
  • Bought between 100 and 499 items
  • Exclusive Author
  • Referred between 500 and 999 users
+5 more
KrownThemes says

LOL!!! :D

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

491 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Won a Competition
  • Referred between 1000 and 1999 users
  • Author had a Free File of the Month
  • Author had a File in an Envato Bundle
  • Bought between 10 and 49 items
+3 more
pixelentity says

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

BF

490 posts
  • Sold between 10 000 and 50 000 dollars
  • Referred between 10 and 49 users
  • Bought between 50 and 99 items
  • Exclusive Author
  • Has been a member for 3-4 years
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?
490 posts
  • Sold between 10 000 and 50 000 dollars
  • Referred between 10 and 49 users
  • Bought between 50 and 99 items
  • Exclusive Author
  • Has been a member for 3-4 years
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.

490 posts
  • Sold between 10 000 and 50 000 dollars
  • Referred between 10 and 49 users
  • Bought between 50 and 99 items
  • Exclusive Author
  • Has been a member for 3-4 years
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
3439 posts Ruben Bristian
  • Most Wanted Bounty Winner
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Has been a member for 6-7 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Won a Competition
  • Bought between 100 and 499 items
  • Exclusive Author
  • Referred between 500 and 999 users
+5 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.

491 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Won a Competition
  • Referred between 1000 and 1999 users
  • Author had a Free File of the Month
  • Author had a File in an Envato Bundle
  • Bought between 10 and 49 items
+3 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;
490 posts
  • Sold between 10 000 and 50 000 dollars
  • Referred between 10 and 49 users
  • Bought between 50 and 99 items
  • Exclusive Author
  • Has been a member for 3-4 years
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 = ""; 
491 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Won a Competition
  • Referred between 1000 and 1999 users
  • Author had a Free File of the Month
  • Author had a File in an Envato Bundle
  • Bought between 10 and 49 items
+3 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