-2

I'm a beginner and I have two sets of "if statements" that are applying to a form and they almost do the same thing but I want to understand why they do what they do. When I print the errors and submit an empty form, the first if statement echos each field in the array with the words "is requred" next to each field. As for the 2nd if statement, it simply echos "You didn't fill in all of the categories" if even one field was left empty in the form compared to the first if statement that echos the specific field that was left empty.Like if you could break down each set of coding for me step by step that would be great. And could you possibly recommend a link where I can learn the language? Thank you!

<?php

    if(isset($_POST['registrationform'])){
        $required_fields = array('first_name', 'last_name', 'email', 'password', 'gender', 'month', 'day', 'year');
        foreach ( $required_fields as $key=>$value) {
         if (!isset($_POST[$value]) || $_POST[$value]=='') {
              $errors[$value] =$key." is required";
            }
            }  
            }
            print_r($errors);
?>   



<?php
if (empty($_POST) === false) {
        $required_fields = array('first_name', 'last_name', 'email', 'password', 'sex', 'birthday', 'terms_of_service');
        foreach ($_POST as $key=>$value) {
            if (empty($value) && in_array($key, $required_fields) === true) {
                $errors[] = 'You didn\'t fill in all of the categories.';
                break 1;
            }
            }
            }
            print_r($errors);
?>
Kacy Raye
  • 9
  • 2
  • 5

1 Answers1

0

"isset() returns true if the variable exists, even if it's empty. empty() returns true if the variables doesn't exist OR if their contents are considered to be 'empty' (empty string, 0, NULL, false, array with no elements). They're quite different, really. I believe empty($var) is equivalent to !($var)."

http://www.webhostingtalk.com/archive/index.php/t-373975.html

See this Question: What is the difference between isset and empty?

Information Regarding both functions:

http://us3.php.net/manual/en/function.empty.php

http://us3.php.net/manual/en/function.isset.php

Tutorial for learning php:

http://www.w3schools.com/php/default.asp

Community
  • 1
  • 1
Floris Velleman
  • 4,848
  • 4
  • 29
  • 46