INTRODUCTION TO PHP AND MYSQL - CONDITIONAL CONSTRUCTS WITH "IF"
In the exercise, you will be working within the Introduction to PHP folder. If you have not already done so, create a new Dreamweaver site pointing to the Introduction to PHP folder. You will need to add Remote Info and Testing Server information, and create a new folder on the server Introduction_to_PHP to which you will publish.
Conditional constructs are the classic programming structures that enable you to have one or more statements executed only in the event a certain condition is true. In PHP, we use the following syntax to build conditionals:
if(condition){
statement(s);
}elseif(other condition) {
statement(s);
}elseif(other condition) {
statement(s);
}else{
statement(s);
}
These expressions take the following form, as shown in demos/conditionals.php:
<!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">
<head>
<title>Conditional Structures</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css">
</style>
</head>
<body>
<h2>Hello World! Let me wish you a gracious
<?php
if(date("G") < 12) {
echo "Good Morning!";
}elseif (date("G") < 18) {
echo "Good Afternoon!";
}else {
echo "Good Evening!";
}
?>
</h2>
<p>The time now is <?php echo date("g:i:s a");?> on <?php echo date("d/n/Y");?</p>
</body>
</html>
Requesting the page after 6.00pm returns a response similar to:
This PHP uses the date string to return an integer between 0 and 23 indicating the hour on the server.
<?php
if(date("G") < 12) {
echo "Good Morning!";
}elseif(date("G") < 18) {
echo "Good Afternoon!";
}else{
echo "Good Evening!";
}
?>
The else and elseif clauses are optional. You may have as many elseif clauses as you want.
Alternative Syntax for Conditionals
Some people do not like all the curly brackets. PHP provides a way out by using colons and the keyword endif:
if(weather == "sunny"):
statement;
statement;
elseif(weather == "snowy"):
statement;
statement;
else:
statement;
statement;
endif;
Comparison Operators in PHP
A comparison operator defines the nature of a comparison. PHP uses the following comparison operators:
Operator / Definition= = / Equals
! = / Does not equal
Greater than
>= / Greater than or equal to
Less than
<= / Less than or equal to
The Logical AND (&) and OR (||)
The logical And (&) and Or (||) operators can be used to join clauses in a conditional expression. When clauses are joined with , all of the joined clauses must evaluate to true for the entire expression to evaluate to true. When you join clauses with ||, if any one of the joined clauses evaluates to true, the entire expression is evaluated to true.
As an example, publish and request demos/andorform.php, to display the following form.
Submit the form several times, checking different combinations of checkboxes, or checking no checkboxes at all. The response will be dynamically customised depending on which combination of checkboxes has been sent to the server. For example, if you submit without checking any checkbox (you have never used WavaBeans), this will be returned:
If you check only the first checkbox and submit, the message returned will be:
We are sure you find the Super-Duper 10-Pound Box economical and long-lasting!!
And, to show our special thanks to you for using WavaBeans, we will be sending you an all-in-one Wava Soap Packet, or WSP, as a special gift!
If you check only the second checkbox and submit, the message returned will be:
We are sure you find the Handy-Dandy 2-Pound Box convenient for travel and school.!
And, to show our special thanks to you for using WavaBeans, we will be sending you an all-in-one Wava Soap Packet, or WSP, as a special gift!
And if you check both checkboxes and submit, the message returned will be:
Thank you for being a loyal customer who uses all the sizes of WavaBeans!
And, to show our special thanks to you for using WavaBeans, we will be sending you an all-in-one Wava Soap Packet, or WSP, as a special gift!
The content of the first paragraph of the response is determined by a conditional with several clauses, while the content of the second paragraph, is determined by a condition using an || clause.
The appropriate portion of andorform.php is shown below:
<form method="post" action="andor.php">
<table>
<tr>
<td>Super-Duper 10-Pound Box</td>
<td<input type = "checkbox" name = "large" /</td>
</tr>
<tr>
<td>Handy-Dandy 2-Pound Box</td>
<td<input type = "checkbox" name = "small" /</td>
</tr>
</table>
<div>
<input type = "submit" value = "Submit the Survey" />
<input type = "reset" value = "Start Over" />
</div>
</form>
The portion of andor.php that handles the response is shown below:
<!--This if block decides which first paragraph to display-->
<?php
if((isset($_POST["small"]) == false) & (isset($_POST["large"]) == false))
{
?>
<p>You are missing out on a Laundry Day Miracle. Let us send you a coupon so you can discover the <strong>Wonder of WavaBeans</strong>!</p>
<?php
}
elseif((isset($_POST["small"]) == true) & (isset($_POST["large"]) == false))
{
?>
<p>We are sure you find the <strong>Handy-Dandy 2-Pound Box</strong> convenient for travel and school.</strong>!</p>
<?php
}
elseif((isset($_POST["small"]) == false) & (isset($_POST["large"]) == true))
{
?>
<p>We are sure you find the <strong>Super-Duper 10-Pound Box</strong> economical and long-lasting!</strong>!</p>
<?php
}
elseif((isset($_POST["small"]) == true) & (isset($_POST["large"]) == true))
{
?>
<p>Thank you for being a loyal customer who uses all the sizes of <strong>WavaBeans</strong>!</p>
<?php
}
?>
<!--This if block with an Or condition returns its paragraph if any combination is checked-->
<?php
if((isset($_POST["small"]) == true) || (isset($_POST["large"]) == true) )
{
?>
<p>And, to show our special thanks to you for using <strong>WavaBeans</strong>, we will be sending you an all-in-one <strong>Wava Soap Packet</strong>, or <strong>WSP</strong>, as a special gift!</p>
<?php
}
?>
One thing to notice here is a function called isset(). This function checks to see whether a variable exists. When a checkbox is not selected, no value for that form field is submitted to the server. The isset() function will return true or false. There is actually a shortcut for checking for true and false values, which can be seen in demos/andor-abbreviated.php:
<!--This if block decides which first paragraph to display-->
<?php
if(!isset($_POST["small"]) & !isset($_POST["large"]))
{
?>
<p>You are missing out on a Laundry Day Miracle. Let us send you a coupon so you can discover the <strong>Wonder of WavaBeans</strong>!</p>
<?php
}
elseif(isset($_POST["small"]) & !isset($_POST["large"]))
{
?>
<p>We are sure you find the <strong>Handy-Dandy 2-Pound Box</strong> convenient for travel and school.</strong>!</p>
<?php
}
elseif(!isset($_POST["small"]) & isset($_POST["large"]))
{
?>
<p>We are sure you find the <strong>Super-Duper 10-Pound Box</strong> economical and long-lasting!</strong>!</p>
<?php
}
elseif(isset($_POST["small"]) & isset($_POST["large"]))
{
?>
<p>Thank you for being a loyal customer who uses all the sizes of <strong>WavaBeans</strong>!</p>
<?php
}
?>
<!--This if block with an Or condition returns its paragraph if any combination is checked-->
<?php
if(isset($_POST["small"]) || isset($_POST["large"]))
{
?>
<p>And, to show our special thanks to you for using <strong>WavaBeans</strong>, we will be sending you an all-in-one <strong>Wava Soap Packet</strong>, or <strong>WSP</strong>, as a special gift!</p>
<?php
}
?>
The other new thing here is the variable:
$_POST["small"]
This variable is a special one for reading values submitted with forms (specifically, forms with a method of POST). You will learn more about this later. Suffice it to say that you just put the name of the form field in square brackets and parentheses after the $_POST, and you can get a form field’s value.
A - Introduction to PHP and MySQL - Conditional Constructs with If
Page 9 of 9 Version 1