Jump to content

[SOLVED] ISSET(...) ? An easy one folks!


gr1zzly

Recommended Posts

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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  ;)

Link to comment
Share on other sites

<?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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.