nivekz Posted September 14, 2011 Share Posted September 14, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/247144-need-help-with-isset/ Share on other sites More sharing options...
AbraCadaver Posted September 14, 2011 Share Posted September 14, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/247144-need-help-with-isset/#findComment-1269313 Share on other sites More sharing options...
ManiacDan Posted September 14, 2011 Share Posted September 14, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/247144-need-help-with-isset/#findComment-1269320 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.