gr1zzly Posted September 30, 2009 Share Posted September 30, 2009 Hey folks, I was just wondering what the difference between if ( isset($_POST['validation']) ) { and if ( $_POST['validation'] ) { is? Surely the second statement is still equivalent to a true/false test for the presence of the given variable? Sorry for the noobalicious question people but curiosity got the better of me. I've always used isset() because that's what I learned but the other day I accidentally left it out (tiredness - lol) but the function still worked! So I thought I'd ask you guy's. Quote Link to comment https://forums.phpfreaks.com/topic/176030-solved-isset-an-easy-one-folks/ Share on other sites More sharing options...
trq Posted September 30, 2009 Share Posted September 30, 2009 if ( $_POST['validation'] ) { Will throw a NOTICE if $_POST['validation'] doesn't exist. Quote Link to comment https://forums.phpfreaks.com/topic/176030-solved-isset-an-easy-one-folks/#findComment-927539 Share on other sites More sharing options...
marvelade Posted September 30, 2009 Share Posted September 30, 2009 your 2 snippets checks 2 different things, but since PHP is such a forgiving language (except when u forget a semicolon ;-) ) it will evaluate the existance of a variable as not false and the abscence of it as not true. Other languages would just say "argument should be of type BOOLEAN" or something similar. PHP does its best to try to figure out what you mean if it's not what it expects. grtz, Marv Quote Link to comment https://forums.phpfreaks.com/topic/176030-solved-isset-an-easy-one-folks/#findComment-927546 Share on other sites More sharing options...
cags Posted September 30, 2009 Share Posted September 30, 2009 It is important to note they won't always give the same result. If for example you give validation the value of 0, the first statment will calculate isset as TRUE and as such will perform the code inside the if block. The second statement will take the value of validation to be FALSE and therefore the code within the if block will not be performed. Quote Link to comment https://forums.phpfreaks.com/topic/176030-solved-isset-an-easy-one-folks/#findComment-927564 Share on other sites More sharing options...
gr1zzly Posted September 30, 2009 Author Share Posted September 30, 2009 So in the 2nd example php effectively guesses that I'm testing for a boolean response based on the presence or lack of for the given variable? Or --> Thorpe do you mean "even if $_POST['validation'] doesn't exist" ? Which would imply to me that in this case the conditional statement is incomplete, i.e. if ( $_POST... = 'something' ) would work, but since $_POST on it's own has nothing to qualify it it will always be 'true' - sort of Quote Link to comment https://forums.phpfreaks.com/topic/176030-solved-isset-an-easy-one-folks/#findComment-927565 Share on other sites More sharing options...
cags Posted September 30, 2009 Share Posted September 30, 2009 <?php if ( isset($_POST['validation']) ) { ?> This code checks if the $_POST array contains an element with the associative id of 'validation'. <?php if ( $_POST['validation'] ) { ?> This code checks if the value of $_POST['validation'] is equal to TRUE or FALSE. Because of the 'easy going nature' of PHP, if you make a boolean comparison of any variable (ie check if it is TRUE of FALSE), it will always return TRUE if it has a value and that value doesn't explicitly equal FALSE (ie 0). Otherwise it will return FALSE. As stated in my previous post, if 0 is ever an acceptable value and you don't use the isset() method, you can end up with a different result to what you wanted. As such you should alway use isset() for this type of check. Quote Link to comment https://forums.phpfreaks.com/topic/176030-solved-isset-an-easy-one-folks/#findComment-927569 Share on other sites More sharing options...
gr1zzly Posted September 30, 2009 Author Share Posted September 30, 2009 Thank for the clarification Cags, really appreciated. -> can't believe I didn't notice this difference before - lols. Quote Link to comment https://forums.phpfreaks.com/topic/176030-solved-isset-an-easy-one-folks/#findComment-927610 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.