Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this an OK test to see if a variable is set

Yesterday, I posted an answer to a question that included several (unknown to me at the time) very bad code examples. Since then, I've been looking at my fundamental knowledge of PHP that allowed me to think that such code is possible. This brings me to a question that I can't seem to find an answer to:

If I want to check for whether or not a variable has anything set, is it a valid practice to not use isset() or another helper function? here's a "for instance":

if($not_set){
    //do something
} else {
    //do something else
}

Rather than...

if(isset($not_set)){
    //do something
} else {
    //do something else
}

From the name of the variable, you can see that this variable is not set. Therefore the conditional would be false and the else portion would run. Up until now I have been using this practice, but after the posts yesterday, I now have an inkling that this is wrong.

Here's why I thought that it would be an ok practice to leave out the isset() function above. From PHP manual:

The if construct is one of the most important features of many languages, PHP included. It allows for conditional execution of code fragments. PHP features an if structure that is similar to that of C:

if (expr) statement

As described in the section about expressions, expression is evaluated to its Boolean value. If expression evaluates to TRUE, PHP will execute statement, and if it evaluates to FALSE - it'll ignore it. More information about what values evaluate to FALSE can be found in the 'Converting to boolean' section.

And from the 'Converting to boolean section':

When converting to boolean , the following values are considered FALSE:

... * the special type NULL (including unset variables)

Why would the manual go out of its way to state that unset variables are included if this is a bad practice? If it's unset, it gets converted to NULL and therefore is evaluated properly by the conditional. Using isset() will find the same result, but will take extra cycles to do so.

Can someone please enlighten me as to whether I've been wrong this whole time and why? (And just how bad it is, maybe?)

Thanks, SO, you never disappoint.

Edit: Thanks everyone (and that was quick). I honestly think all the answers so far are great and don't know which to pick for the answer... If yours isn't picked, I will still upvote :o)

like image 554
TCCV Avatar asked Jul 20 '10 23:07

TCCV


2 Answers

If the variable is not set you get a Notice. If you use isset() you don't get a notice. So from an error reporting point of view, using isset() is better :)

Example:

error_reporting(E_ALL);   
if($a) {
   echo 'foo';
}

gives

Notice: Undefined variable: a in /Users/kling/test on line 5

whereas

error_reporting(E_ALL);
if(isset($a)) {
   echo 'foo';
}

does not output anything.


The bottom line: If code quality is important to you, use isset().

like image 146
Felix Kling Avatar answered Sep 19 '22 01:09

Felix Kling


It's okay but not good practice to use if to check for a set variable. Two reasons off the top of my head:

  1. Using isset makes the intent clear - that you're checking whether the variable is set, and not instead checking whether a condition is true.
  2. if ($not_set) will evaluate to false when $not_set is actually set but is equal to boolean false.
like image 40
casablanca Avatar answered Sep 22 '22 01:09

casablanca