subnet_rx Posted August 17, 2007 Share Posted August 17, 2007 Is there an advantage to using isset() over if() if your wanting to know if a variable has a value? Quote Link to comment https://forums.phpfreaks.com/topic/65493-if-or-isset/ Share on other sites More sharing options...
wildteen88 Posted August 17, 2007 Share Posted August 17, 2007 Is there an advantage to using isset() over if() if your wanting to know if a variable has a value? isset does not check variables values. isset only checks whether a variable exists or not. Quote Link to comment https://forums.phpfreaks.com/topic/65493-if-or-isset/#findComment-326996 Share on other sites More sharing options...
roopurt18 Posted August 17, 2007 Share Posted August 17, 2007 AFAIK isset() is a function while if is a language construct, which means you can use isset() in places where you can't use if. Quote Link to comment https://forums.phpfreaks.com/topic/65493-if-or-isset/#findComment-326999 Share on other sites More sharing options...
subnet_rx Posted August 17, 2007 Author Share Posted August 17, 2007 So basically, I'm running code if a variable has a value. if($v)... Is that the best way or should I be doing this: if(isset($v)) Quote Link to comment https://forums.phpfreaks.com/topic/65493-if-or-isset/#findComment-327013 Share on other sites More sharing options...
roopurt18 Posted August 17, 2007 Share Posted August 17, 2007 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 } Quote Link to comment https://forums.phpfreaks.com/topic/65493-if-or-isset/#findComment-327023 Share on other sites More sharing options...
wildteen88 Posted August 17, 2007 Share Posted August 17, 2007 I'd use the third method: if(isset($var) && $var){ // If the var exists and evaluates true } For use with variables set from the client, eg _GET, _POST and _COOKIE. Quote Link to comment https://forums.phpfreaks.com/topic/65493-if-or-isset/#findComment-327026 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.