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);
?>