Possible Duplicate:
Regarding if statements in PHP
In PHP scripts – what does an if statement like this check for?
<?php if($variable){ // code to be executed } ?>
I’ve seen it used in scripts several times, and now I really want to know what it “looks for”. It’s not missing anything; it’s just a plain variable inside an if statement… I couldn’t find any results about this, anywhere, so obviously I’ll look stupid posting this.
The construct
if ($variable)tests to see if$variableevaluates to any “truthy” value. It can be a booleanTRUE, or a non-empty, non-NULL value, or non-zero number. Have a look at the list of boolean evaluations in the PHP docs.From the PHP documentation:
Note however that
if ($variable)is not appropriate to use when testing if a variable or array key has been initialized. If it the variable or array key does not yet exist, this would result in anE_NOTICE Undefined variable $variable.