/    Sign up×
Community /Pin to ProfileBookmark

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:

[code=php]
<?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′]}’.nnSincerely,nUs”;
mail ($_POST[’email’], ‘Thank you for registering!’, $body, ‘From: [email protected]’);

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

[/code]

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogOct 19.2007 — It's just saying that if the $errors array is empty -- in other words no errors have been found so far -- then go ahead and and process the input, otherwise (else) output an error message.

Basically, if/elseif/else structures read through each subsequent if and elseif condition until one is found that evaluates as true. At that point, the following code block associate with that condition is processed, then processing falls out of the entire if/elseif/else sequence. If none of the if or elseif conditions evaluate as true, then the code block following the else statement is executed (if there is an assoicated else statement, as it's not required).

So in this case the entire condition section consists of one "if" followed by one "else":
[code=php]
if(condition is true) {
// do this
}
else {
// otherwise do this if condition is false
}
[/code]

More complex structures might have several conditions to try:
[code=php]
if(condition1 is true) {
// do this
}
elseif(condition2 is true) {
// do that
}
elseif(condition3 is true) {
// do the other thing
}
else {
// give up and go home
}
[/code]

Any clearer, or still muddy?
Copy linkTweet thisAlerts:
@webdev077authorOct 20.2007 — Hello NogDog:

Thanks for your detailed explanation on nested if-else structure.

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

webdev077

10/20/07
Copy linkTweet thisAlerts:
@NogDogOct 20.2007 — I personally prefer to keep all the ifs, elseifs, elses, and their respective braces each on a separate line; as I find it easier for my eyes to follow the flow that way:
[code=php]
if($condition)
{
if($test)
{
// do this
}
else
{
// do that
}
}
elseif($otherCondition)
{
// do the other thing
}
else
{
// forget about it
}
[/code]
×

Success!

Help @webdev077 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...