Jump to content

Recommended Posts

guys this thing called isset() is bugging me.What is the difference in when you say

if(isset($_POST['name']))

and

if($_POST['name'])

and

if(!$_POST['name'])

 

and why would someone require a code such as

if($_POST['submit']=='Submit')

 

And what is that code which tells you the user has entered something into the form

Thanks People

Link to comment
https://forums.phpfreaks.com/topic/247144-need-help-with-isset/
Share on other sites

If the form was not submitted (someone just navigates to the processing page) then the var will not be set.  In the case of text inputs and others, if the form is submitted and the field is blank then it will be set but it will be empty, so you can use !empty().  In the case of check boxes, if they are not checked then they will not be set.

isset() returns true or false and doesn't throw a PHP warning when you try to check a variable that doesn't exist.  This gives you two benefits:

1)  An absolute true/false expression to put in your conditional.

2)  No warnings generated

 

As a side comment:

3)  Strings and integers can be SET but still return FALSE when checked inside an IF.

 

So to comment on all your examples:

 

if(isset($_POST['name']))

This is the correct way to do this.  It throws no warnings and will always act exactly like you expect.

 

if($_POST['name'])

If $_POST['name'] is not set, this code will throw a warning.  Also, if $_POST['name'] is SET but EMPTY, this code will return false and the contents of the IF won't be processed.

 

if(!$_POST['name'])

Again, this will throw a warning, and the execution plan is the opposite of the one above (if the string is unset or empty, the IF will execute)

 

if($_POST['submit']=='Submit')

Aside from throwing a warnings if $_POST['submit'] isn't set, this code will only succeed if the value is the word "Submit".  It won't succeed if the value is anything else.  This is used to make sure a specific form was submitted or to ensure that nothing has tampered with the data (though this is a poor example of that).

 

-Dan

 

 

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.