westminster86 Posted March 4, 2008 Share Posted March 4, 2008 is there a way of checking all my variables have a value. I know about using the isset function e.g if (isset($_post['value'])) can i write a single statement that sees if all my variables have values? or do i have to write them out as ive given above one by one? Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted March 4, 2008 Share Posted March 4, 2008 try using empty() instead Quote Link to comment Share on other sites More sharing options...
Trium918 Posted March 4, 2008 Share Posted March 4, 2008 Try this! <?php // check forms filled in if (!filled_out($_POST)) { echo "You have not filled the form out correctly - please go back" ." and try again."; exit; } function filled_out($form_vars) { // test that each variable has a value foreach ($form_vars as $key => $value) { if (!isset($key) || ($value == "")) return false; } return true; } ?> Quote Link to comment Share on other sites More sharing options...
discomatt Posted March 4, 2008 Share Posted March 4, 2008 Uhhh, there's no way to do that. The only way to do that is if you store all your variable names into an array, then use a foreach to iterate through the array and check each one There's no way for PHP to know what vars you want/don't want without telling it which ones are supposed to be there Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted March 4, 2008 Share Posted March 4, 2008 another good thing to do, would be to remove all spaces, and make sure that there are numeric and non-numeric values entered, because with empty, it looks to see if there is anything in the string, and a space does count so it will then return true. isset only checks to see if a variable is set, not if the variable holds anything. Example 1: <?php $cat = ""; isset($cat); // returns true ?> Example 2: <?php $dog = "123121"; isset($dog); // returns true ?> Example 3: <?php $dog = "123121"; isset($bird); // returns false ?> Quote Link to comment Share on other sites More sharing options...
discomatt Posted March 4, 2008 Share Posted March 4, 2008 When dealing with POST variables, if the input is empty, the array key will not be created. I've never seen it act differently Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted March 4, 2008 Share Posted March 4, 2008 it gets created.... it just is blank it would look like this, where fish is the empty value: cat=1&dog=2&fish=&bird=3 Quote Link to comment Share on other sites More sharing options...
discomatt Posted March 4, 2008 Share Posted March 4, 2008 As i said, when dealing with POST variables. The method you highlighted would be GET Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted March 4, 2008 Share Posted March 4, 2008 That is how a POST looks too when you look at the headers. Quote Link to comment 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.