Reputation: 17
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
.
Upvotes: 0
Views: 39
Reputation: 3875
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;
}
Upvotes: 1
Reputation: 1189
Try this;
if(isset($GLOBALS["tpl_loaded"]) && !empty($GLOBALS["tpl_loaded"]))
{
echo 'You need to run this function after <b>load_template</b>!';die;
}
Upvotes: 1