1

I believe I am a bit confused with the isset function in PHP. I'm trying to use this function to determine is a field in a form is null... I was under the impressions that the isset function checks to see if a field has a value in it... but I believe the isset function only determines if the thing passed to it exists.

For example.

If I have a form input field with the name attribute set to "day". I would use isset($_GET['day']); to determine if the form input field is not null? Or does isset just check to see if the 'day' exists and doesn't check that value that it passes?

Any help would be great! Thanks!

1
  • 1
    isset is not a function, but a language construct Commented Jul 22, 2012 at 5:19

3 Answers 3

5

From the PHP isset() page:

[isset() determines] if a variable is set and is not NULL.

This means that:

$var1;
$var2 = NULL;
$var3 = 0;
$var4 = 'test';
isset($var1); //false
isset($var2); //false
isset($var3); //true; note that if($var3) still returns false
isset($var4); //true
isset($var0); //false

To sum up that bit up there, isset() returns FALSE if the variable is either not set (hence the function name) or contains NULL as a value. For isset() to return true, a variable has to both exist and contain an actual value (boolean false included).

In your case with $_GET['day'], you can use isset() to check if a value has actually been passed to it (i.e., that it's not null).

Sign up to request clarification or add additional context in comments.

3 Comments

Yes but if the input is empty, it will in fact pass a empty string '', so isset will return true, and you have to check with empty.
Good point. But as far as I'm aware, the default behaviour of empty fields is to not send a $_GET variable at all.
I'm not sure, but maybe you're right. I personally use empty instead of isset since I often use Ajax and javascript to get values of input so I send it even if it's empty.
3

Isset just check if a variable is defined. Use empty to know if it is null or undefined or something like that.

if(!empty($_GET['day']))
    //Stuff

2 Comments

Alright, so the isset function will always evaluate to false if you visit the form action seperately - for instance if I have the action in post.php and just typed the url to post.php in the browser it will evaluate to false. The only way it evaluates true is if the form is posted and the name value is set for the input field? Thanks for all the responses :)
Correct. However, don't use isset to check if your visitor come from your other page as anyone can do a fake form with the same input names that lead to your page post.php. But it prevent them for accessing the page directly, true.
0

Absolutely right, the isset() function just checks it the variable 'day' exists. In order to check if the value exist or is empty you have to use empty() function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.