eldan88 Posted May 18, 2012 Share Posted May 18, 2012 Hi, I was going to through a PHP tutorial, and came across a form validation user defined function . Below is an example.. function check_required_fields($fields){ $field_errors = array(); foreach ($fields as $requried_fields) { if (!isset($_POST[$requried_fields]) || (empty($_POST[$requried_fields]) && $_POST[$requried_fields] != 0)) if (!isset($_POST[$fieldname]) || (empty($_POST[$fieldname]) && $_POST[$fieldname] != 0)) { $errors[] = $requried_fields; } } return $field_errors; } When the user defined function was called they added an $_POST and the end of the user defined function. Here is a the code snipped of how it looks $errors = array(); // perform validations on the form data $required_fields = array('username', 'password'); $errors = array_merge($errors, check_required_fields($required_fields, $_POST)); If the validation can be preformed with out the $_POST, why bother inserting it? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/262714-why-add-_post-at-the-end-of-a-validation-function/ Share on other sites More sharing options...
creata.physics Posted May 18, 2012 Share Posted May 18, 2012 Hello eldan88. Since check_required_fields() is a custom function with its own paramaters made by the creator, it will only required those paramaters unless otherwise stated. So having $_POST as the second paramater when the function is being called is pointless and does nothing, you can remove it if that's what you're getting at. Quote Link to comment https://forums.phpfreaks.com/topic/262714-why-add-_post-at-the-end-of-a-validation-function/#findComment-1346504 Share on other sites More sharing options...
eldan88 Posted May 18, 2012 Author Share Posted May 18, 2012 Hello eldan88. Since check_required_fields() is a custom function with its own paramaters made by the creator, it will only required those paramaters unless otherwise stated. So having $_POST as the second paramater when the function is being called is pointless and does nothing, you can remove it if that's what you're getting at. I see ... thanks Quote Link to comment https://forums.phpfreaks.com/topic/262714-why-add-_post-at-the-end-of-a-validation-function/#findComment-1346510 Share on other sites More sharing options...
mrMarcus Posted May 18, 2012 Share Posted May 18, 2012 I'm trying to understand what this set of conditions does: if (!isset($_POST[$requried_fields]) || (empty($_POST[$requried_fields]) && $_POST[$requried_fields] != 0)) Check if it's not set OR is empty && does not equal 0. The last 2 cancel eachother out. You're checking to see if it's empty and also if it's not empty. And nowhere in that function is $fieldname set, yet you're using it in your second condition block. Quote Link to comment https://forums.phpfreaks.com/topic/262714-why-add-_post-at-the-end-of-a-validation-function/#findComment-1346604 Share on other sites More sharing options...
scootstah Posted May 18, 2012 Share Posted May 18, 2012 if (!isset($_POST[$requried_fields]) || (empty($_POST[$requried_fields]) && $_POST[$requried_fields] != 0)) Check if it's not set OR is empty && does not equal 0. The last 2 cancel eachother out. You're checking to see if it's empty and also if it's not empty. empty() will return true if there is a value of 0. So, if you want to allow the value "0" you can't rely on empty(). Although since there is no strict comparison to 0, it is still flawed... since empty == 0. It should be: empty($_POST[$requried_fields]) && $_POST[$requried_fields] !== 0) Quote Link to comment https://forums.phpfreaks.com/topic/262714-why-add-_post-at-the-end-of-a-validation-function/#findComment-1346608 Share on other sites More sharing options...
mrMarcus Posted May 18, 2012 Share Posted May 18, 2012 if (!isset($_POST[$requried_fields]) || (empty($_POST[$requried_fields]) && $_POST[$requried_fields] != 0)) Check if it's not set OR is empty && does not equal 0. The last 2 cancel eachother out. You're checking to see if it's empty and also if it's not empty. empty() will return true if there is a value of 0. So, if you want to allow the value "0" you can't rely on empty(). Although since there is no strict comparison to 0, it is still flawed... since empty == 0. It should be: empty($_POST[$requried_fields]) && $_POST[$requried_fields] !== 0) Ya, I know all that I don't see why a username or password would have a value of 0 though, which is why I don't believe his/her intentions were to allow a value of 0. Wouldn't make any sense, anyways. Feel like I'm talking myself into a circle lol. Assuming zero(0) is not an option, that statement should only be checking the isset() and empty() on $_POST[$required_fields]. Without proper parenthesis to give distinction on which conditions should fire when, you're left with the natural order of precedence (&& before ||). Having " .. && $_POST[$required_fields] != 0 .. " in conjunction with the isset() and empty(), and having && take precedence over the || you're left with, basically (rewritten): if ((empty($_POST[$required_fields]) && ($_POST[$required_fields] != 0)) || !isset($_POST[$required_fields])) { Which, in Layman's terms, is like saying: $_POST[$required_fields] must be empty AND must contain a value. I'm basing my thoughts off of the code as I see it, not how I try and decode it in my head. Had they used a strict comparison on the final condition, it would have been easier to see what was going on and their intentions of that condition. Don't take this as a pissing contest, I just like a little back-and-forth problem solving from time-to-time Quote Link to comment https://forums.phpfreaks.com/topic/262714-why-add-_post-at-the-end-of-a-validation-function/#findComment-1346612 Share on other sites More sharing options...
scootstah Posted May 18, 2012 Share Posted May 18, 2012 I don't see why a username or password would have a value of 0 though, which is why I don't believe his/her intentions were to allow a value of 0. Wouldn't make any sense, anyways. I believe the function is supposed to be a general purpose function, not limited to just this snippet. Quote Link to comment https://forums.phpfreaks.com/topic/262714-why-add-_post-at-the-end-of-a-validation-function/#findComment-1346615 Share on other sites More sharing options...
eldan88 Posted May 20, 2012 Author Share Posted May 20, 2012 the 0 was a radio button value for visible or not visible. 1 for visible 0 for not visible I took this snippet on a little tutorial project i am working on. Since 0 is considered empty i make sure its also not equal to 0 in order for it to validate. Quote Link to comment https://forums.phpfreaks.com/topic/262714-why-add-_post-at-the-end-of-a-validation-function/#findComment-1346938 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.