ThemeForest

help me to solve prevent_notice function problem

382 posts
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 2-3 years
  • Referred between 1 and 9 users
  • Sold between 5 000 and 10 000 dollars
  • Vietnam
rongcon 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!

2557 posts Nice Guy
  • Most Wanted Bounty Winner
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Has been a member for 5-6 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Won a Competition
  • Bought between 100 and 499 items
  • Exclusive Author
  • Referred between 200 and 499 users
+5 more
RubenBristian says

LOL!!! :D

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

404 posts
  • Elite Author
  • Sold between 100 000 and 250 000 dollars
  • Won a Competition
  • Referred between 500 and 999 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

382 posts
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 2-3 years
  • Referred between 1 and 9 users
  • Sold between 5 000 and 10 000 dollars
  • Vietnam
rongcon 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?
382 posts
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 2-3 years
  • Referred between 1 and 9 users
  • Sold between 5 000 and 10 000 dollars
  • Vietnam
rongcon 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.

382 posts
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 2-3 years
  • Referred between 1 and 9 users
  • Sold between 5 000 and 10 000 dollars
  • Vietnam
rongcon 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
2557 posts Nice Guy
  • Most Wanted Bounty Winner
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Has been a member for 5-6 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Won a Competition
  • Bought between 100 and 499 items
  • Exclusive Author
  • Referred between 200 and 499 users
+5 more
RubenBristian 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.

404 posts
  • Elite Author
  • Sold between 100 000 and 250 000 dollars
  • Won a Competition
  • Referred between 500 and 999 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;
382 posts
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 2-3 years
  • Referred between 1 and 9 users
  • Sold between 5 000 and 10 000 dollars
  • Vietnam
rongcon 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 = ""; 
404 posts
  • Elite Author
  • Sold between 100 000 and 250 000 dollars
  • Won a Competition
  • Referred between 500 and 999 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