Jump to content

If or isset?


subnet_rx

Recommended Posts

It depends on the context.

 

  function someFunc($arg){
    if($arg){ // Test that the value exists
    }
  }

The example above doesn't make any sense.  The argument $arg isn't optional so it has to exist.

 

  if($value){ // Ensure a value is true
  }

This will work as expected, the if body will only execute if the variable is true.  However, if the variable is not set PHP will raise an error, possibly silently.

 

Use isset() when there is a possibility the variable is not set and then test it's value.

  if(isset($var) && $var){
    // If the var exists and evaluates true
  }

Link to comment
https://forums.phpfreaks.com/topic/65493-if-or-isset/#findComment-327023
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.