Home » Featured, PHP Tutorials, Tutorials

PHP and elseif Conditional Statement

4 March 2009 69 views No Comment


Although, I should have covered else conditional statements instead, I though to myself that should be pretty easy to understand if you were to look for tutorials on that, and so instead I will be talking about elseif conditionals. So what is a elseif condition? In short it is a running if conditional, meaning that it would go through the list of criteria that you have set and so it would look like this.

// Validate the color:
if ($_POST['color'] == 'red') {
	print '<p style="color:red;">Red is your favorite color.</p>';
} elseif ($_POST['color'] == 'yellow') {
	print '<p style="color:yellow;">Yellow is your favorite color.</p>';
} elseif ($_POST['color'] == 'green') {
	print '<p style="color:green;">Green is your favorite color.</p>';
} elseif ($_POST['color'] == 'blue') {
	print '<p style="color:blue;">Blue is your favorite color.</p>';
} else { // Problem!
	print '<p class="error">Please select your favorite color.</p>';
	$okay = FALSE;
}

// If there were no errors, print a success message:
if ($okay) {
	print '<p>You have been successfully registered (but not really).</p>';
	print "<p>You entered your birthday as $birthday.</p>";
}

As complicated as it looks, and for a beginner like me it does, even though I am writing this example, it will help cut down some programming time down if you were to use elseif to cut down on the if else statements that take up a few hundred lines of code. So what little snippet telling us, well first, if the user selects red as their favorite color, then when they complete the form and click submit it will display your favorite color is red. However(else), if your favorite color is blue, yellow or green it will give you a different statement based on the favorite color the user has picked. Though if you were to write it the other way it be a bit longer and so if you use the elseif conditional statement it cleans up some space and makes the script seem shorter.

Ok, we have the form complete from our last example:

body {
	font-family:"Lucida Grande", "Lucida Sans Unicode", Verdana, Arial, Helvetica, sans-serif;
}
p, h1, form, button {
	border:0;
	margin:0;
	padding:0;
}
.spacer {
	clear:both;
	height:1px;
}
/* ----------- My Form ----------- */
.myform {
	margin:0 auto;
	width:400px;
	height:400px;
	padding:14px;
}
/* ----------- stylized ----------- */
#stylized {
	border:solid 2px #b7ddf2;
	background:#ebf4fb;
}
#stylized h1 {
	font-size:14px;
	font-weight:bold;
	margin-bottom:8px;
}
#stylized p {
	font-size:11px;
	color:#666666;
	margin-bottom:20px;
	border-bottom:solid 1px #b7ddf2;
	padding-bottom:10px;
}
#stylized label {
	display:block;
	font-weight:bold;
	text-align:right;
	width:140px;
	float:left;
}
#stylized .small {
	color:#666666;
	display:block;
	font-size:11px;
	font-weight:normal;
	text-align:right;
	width:140px;
}
#stylized input {
	float:left;
	font-size:12px;
	padding:4px 2px;
	border:solid 1px #aacfe4;
	width:200px;
	margin:2px 0 20px 10px;
}
#stylized select {
	float:left;
	font-size:12px;
	padding:4px 0px;
	border:solid 1px #aacfe4;
	width:200px;
	margin:2px 0 20px 10px;
}
#stylized button {
	clear:both;
	margin-left:150px;
	width:125px;
	height:31px;
 background:#666666 text-align:center;
	line-height:31px;
	color:#FFFFFF;
	font-size:11px;
	font-weight:bold;
}
<div style="width:450px;">Please complete this form to register:
  <div style="width:450px;">
    <div id="stylized" class="myform">
      <form action="script_06_07.php" method="post">
        <label>Email Address:<span class="small">Enter Email</span></label>
        <input type="text" name="email" size="30" />
        <label>Password:<span class="small">Enter Password</span></label>
        <input type="password" name="password" size="20" />
        <label>Password:<span class="small">Confirm Password</span></label>
        <input type="password" name="confirm" size="20" />
        <label>Date Of Birth:<span class="small">Enter Month, Day, Year</span> </label>
        <select name="month">
          <option value="">Month</option>
          <option value="1">January</option>
          <option value="2">February</option>
          <option value="3">March</option>
          <option value="4">April</option>
          <option value="5">May</option>
          <option value="6">June</option>
          <option value="7">July</option>
          <option value="8">August</option>
          <option value="9">September</option>
          <option value="10">October</option>
          <option value="11">November</option>
          <option value="12">December</option>
        </select>
        <label>Day: <span class="small">Enter Month, Day, Year</span></label>
        <select name="day">
          <option value="">Day</option>
          <option value="1">1</option>
          <option value="2">2</option>
          <option value="3">3</option>
          <option value="4">4</option>
          <option value="5">5</option>
          <option value="6">6</option>
          <option value="7">7</option>
          <option value="8">8</option>
          <option value="9">9</option>
          <option value="10">10</option>
          <option value="11">11</option>
          <option value="12">12</option>
          <option value="13">13</option>
          <option value="14">14</option>
          <option value="15">15</option>
          <option value="16">16</option>
          <option value="17">17</option>
          <option value="18">18</option>
          <option value="19">19</option>
          <option value="20">20</option>
          <option value="21">21</option>
          <option value="22">22</option>
          <option value="23">23</option>
          <option value="24">24</option>
          <option value="25">25</option>
          <option value="26">26</option>
          <option value="27">27</option>
          <option value="28">28</option>
          <option value="29">29</option>
          <option value="30">30</option>
          <option value="31">31</option>
        </select>
        <label>Year:<span class="small">Enter Month, Day, Year</span></label>
        <input type="text" name="year" value="YYYY" size="4" />
        <label>Favorite Color:<span class="small">Pick Favorite Color</span> </label>
        <select name="color">
          <option value="">Pick One</option>
          <option value="red">Red</option>
          <option value="yellow">Yellow</option>
          <option value="green">Green</option>
          <option value="blue">Blue</option>
        </select>
        <input type="submit" name="submit" value="Register" />
      </form>
    </div>
  </div>
</div>

So our next task is to write up our php script that will include both if/else conditions and elseif conditions as well.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
	<title>Registration</title>
	<style type="text/css" media="screen">
		.error { color: red; }
	</style>
</head>
<body>
<h2>Registration Results</h2>
/* This script receives eight values from register.html:
email, password, confirm, month, day, year, color, submit */

// Address error management, if you want.

// Flag variable to track success:
$okay = TRUE;

// Validate the email address:
if (empty($_POST['email'])) {
	print '<p class="error">Please enter your email address.</p>';
	$okay = FALSE;
}

// Validate the password:
if (empty($_POST['password'])) {
	print '<p class="error">Please enter your password.</p>';
	$okay = FALSE;
}

// Check the two passwords for equality:
if ($_POST['password'] != $_POST['confirm']) {
	print '<p class="error">Your confirmed password does not match the original password.</p>';
	$okay = FALSE;
}

// Validate the birthday:
$birthday = '';

// Validate the month:
if (is_numeric($_POST['month'])) {
	$birthday = $_POST['month'] . '-';
} else {
	print '<p class="error">Please select the month you were born.</p>';
	$okay = FALSE;
}

// Validate the day:
if (is_numeric($_POST['day'])) {
	$birthday .= $_POST['day'] . '-';
} else {
	print '<p class="error">Please select the day you were born.</p>';
	$okay = FALSE;
}

// Validate the year:
if ( is_numeric($_POST['year']) AND (strlen($_POST['year']) == 4) ) {

	// Check that they were born before 2009.
	if ($_POST['year'] >= 2009) {
		print '<p class="error">Either you entered your birth year wrong or you come from the future!</p>';
		$okay = FALSE;
	} else {
		$birthday .= $_POST['year'];
	} // End of 2nd conditional.

} else { // Else for 1st conditional.

	print '<p class="error">Please enter the year you were born as four digits.</p>';
	$okay = FALSE;

} // End of 1st conditional.

// Validate the color:
if ($_POST['color'] == 'red') {
	print '<p style="color:red;">Red is your favorite color.</p>';
} elseif ($_POST['color'] == 'yellow') {
	print '<p style="color:yellow;">Yellow is your favorite color.</p>';
} elseif ($_POST['color'] == 'green') {
	print '<p style="color:green;">Green is your favorite color.</p>';
} elseif ($_POST['color'] == 'blue') {
	print '<p style="color:blue;">Blue is your favorite color.</p>';
} else { // Problem!
	print '<p class="error">Please select your favorite color.</p>';
	$okay = FALSE;
}

// If there were no errors, print a success message:
if ($okay) {
	print '<p>You have been successfully registered (but not really).</p>';
	print "<p>You entered your birthday as $birthday.</p>";
}
</body>
</html>

If you notice the PHP script, you know how long and complicated PHP can get, of course, practice makes perfect and so the difficulty will lesson over time. Thus the reason to learn the basics and just keep working at them until you feel comfortable with PHP and that you understand everything that is going on with a PHP script(s).

Also by looking at the script it does a bit more validation and that it covers pretty much everything in this form and so to test it to make sure it works, fill in everything and click the button, and then fill some stuff in and then click the button to see what kind of errors you can get.

That’s about it really, you understand the basics of how elseif conditionals work and so I suggest exploring what else you can do with conditional statements


Leave your response!

You must be logged in to post a comment.