SakhaliaNetHome PageMilitary HistoryVorKutaAcceptance of cookiesAcceptance of cookies

PHP Tutorial :: Decisions

PHP Example #16

If and else statements

Taking decisions is a fundamental feature in the world of programming. Decision structures evaluate one or several conditions and execute different actions based on them. In previous examples we have seen already these structures in use, but now we will look at them in more detail.

Here the form makes a call to an external script written into a .php file, whose source code can be seen below. The output will be displayed in a blank page, so hit Back to return to this webpage after seeing the result.

Logged (Y)
Logged (N)

<?php
$logged_in = $_POST['logged'];
print "This is login status:\n\n";
if ($logged_in) {
print "Welcome aboard, trusted user.";
} else {
print "Howdy, stranger.";
}
?>

PHP Example #17

If, else and elseif statements

The elseif() statement is useful when we need to evaluate two or more conditions, while the else statement will trigger an action if none of the previous conditions is true. Be aware that only one of the actions can be triggered; the first action that is excecuted will skip any other condition present in the if() statement, no matter if it is true as well.

Here the form makes a call to an external script written into a .php file, whose source code can be seen below. The output will be displayed in a blank page, so hit Back to return to this webpage after seeing the result.

Logged (Admin)
Logged (Moderator)
Logged (RegUser)
Logged (GuestUser)

<?php
$logged_in = $_POST['user'];
print "This is user login:\n\n";
if ($logged_in == "1") {
print "Welcome aboard, Administrator.";
} elseif ($logged_in == "2") {
print "Welcome aboard, Moderator.";
} elseif ($logged_in == "3") {
print "Welcome aboard, Registered User.";
} else {
print "Howdy, Guest User.";
}
?>