简体   繁体   中英

Unable to check if variables are set

if(!isset($GLOBALS["tpl_loaded"]) || isset($GLOBALS["tpl_loaded"]) && $GLOBALS["tpl_loaded"] !== true)
{
    die("You need to run this function after <b>load_template</b>!");
}

This is my add_template function which adds more templates to the loaded one. load_template sets a value after adding the template ( $GLOBALS["tpl_loaded"] = true ) and I want to use add_template only if I run load_template first, but I always get

You need to run...

even if I run load_template .

Try this;

if(isset($GLOBALS["tpl_loaded"]) && !empty($GLOBALS["tpl_loaded"]))
{
    echo 'You need to run this function after <b>load_template</b>!';die;
}

Simply use !empty . It is the best solution to check if the variable is declared and not empty.

isset() checks if a variable has a value including ( False , 0 , or empty string) , but not NULL. Returns TRUE if var exists; FALSE otherwise.

On the other hand the empty() function checks if the variable has an empty value empty string , 0, NULL ,or False. Returns FALSE if var has a non-empty and non-zero value."

if (!empty($GLOBALS["tpl_loaded"]) && ($GLOBALS["tpl_loaded"] !== true)) {
    echo 'You need to run this function after <b>load_template</b>!';
    die;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM