question regarding nested IF-ELSE structure

Hello everyone:

I am doing a exercise from Larry Ullman's book - PHP and MySQL for dynamic web sites. (chapter 3 P119 register.php)

a) I couldn't figure out the if-else structure around if(empty($errors)) section. please see line 40 and line 49

Please help me to understand how it works. Thanks!

The code:

<?php # Script 3.13 - register.php
/*This script is from the book website
-------------- */
$page_title = 'Register';
include ('./includes/header.php');

// Check if the form has been submitted.
if (isset($_POST['submitted'])) {

$errors = array(); // Initialize error array.

// Check for a name.
if (empty($_POST['name'])) {
$errors[] = 'You forgot to enter your name.';
}

// Check for an email address.
if (empty($_POST['email'])) {
$errors[] = 'You forgot to enter your email address.';
}

// Check for a password and match against the confirmed password.
if (!empty($_POST['password1'])) {
if ($_POST['password1'] != $_POST['password2']) {
$errors[] = 'Your password did not match the confirmed password.';
}
} else {
$errors[] = 'You forgot to enter your password.';
}

if (empty($errors)) { // If everything's okay.

// Register the user.

// Send an email.
$body = "Thank you for registering with our site!\nYour your password is '{$_POST['password1']}'.\n\nSincerely,\nUs";
mail ($_POST['email'], 'Thank you for registering!', $body, 'From: admin@site.com');

echo '<h1 id="mainhead">Thank you!</h1>
<p>You are now registered. An email has been sent to your email address confirming the information.</p><p><br /></p>';

} else { /* line 40: to me the left curly braces before else is
the end for if(empty($errors)) */

echo '<h1 id="mainhead">Error!</h1>
<p class="error">The following error(s) occurred:<br />';
foreach ($errors as $msg) { // Print each error.
echo " - $msg<br />\n";
}
echo '</p><p>Please go back and try again.</p><p><br /></p>';

} /* line 49: the book states here is the End of if (empty($errors)).
it doesn't make send to me */

} //end of if(post submit function
else { // Display the form.
?>
<h2>Register</h2>
<form action="register1.php" method="post">
<p>Name: <input type="text" name="name" size="20" maxlength="40" /></p>
<p>Email Address: <input type="text" name="email" size="20" maxlength="40" /> </p>
<p>Password: <input type="password" name="password1" size="10" maxlength="20" /></p>
<p>Confirm Password: <input type="password" name="password2" size="10" maxlength="20" /></p>
<p><input type="submit" name="submit" value="Register" /></p>
<input type="hidden" name="submitted" value="TRUE" />
</form>
<?php
} // Close the main IF-ELSE.
include ('./includes/footer.php');
?>
[3641 byte] By [sjcoder07] at [2007-11-20 11:32:15]
# 1 Re: question regarding nested IF-ELSE structure
I'm not exactly sure what you don't understand, so if I am off, forgive me.

1. The first part of if checks to see if the errors array is empty or has no values. If there are now indexes found, then the mail will be sent.

2. The else statement will fire if there are indexes found within the error array. This will then read the array index by index and output the error.
PeejAvery at 2007-11-10 3:56:09 >
# 2 Re: question regarding nested IF-ELSE structure
The else statement on line 40 is a continuation of the if-statement which ends at line 49. Only the first block (between 'if' and 'else') will execute if $errors is empty (or evaluates to false) and only the second block (between 'else' and line 49) will execute if it is not empty.

$value = 123;

// The following statement...
if ($value == 123) {
// $value is equal to 123.
} else {
// $value is NOT equal to 123.
}

// ...is the same as the following:
if ($value == 123) {
// $value is equal to 123.
}

if ($value != 123) {
// $value is NOT equal to 123.
}
andreasblixt at 2007-11-10 3:57:08 >
# 3 Re: question regarding nested IF-ELSE structure
Hello Peej and andreasblixt:

Thanks for your help. I reviewed the code again. I know understood where the else statement ends. It ends on line 49 (no dount). Before I thought the else statement ended at the foreach statement ( right curly braces just above the echo statement).

sjcoder07
sjcoder07 at 2007-11-10 3:58:13 >