Decisions - Conditionals

 

Introduction.  At many points in time it is important to get different blocks of code to execute based on some criteria or conditions.  For example,
  • maybe you want to give a price break to people thta are placing orders over a particular size
  • maybe you want to treat customers that have done a certain amount of business with you in the past somewhat differently from unknown people
  • maybe you want to allow changes to certain entries to be done only by people who can give a particular password
  • maybe you want a program to execute certain things only during a certain time period
  • on and on

Obviously, this list can continue to grow ad infinitum.

The syntax for decision statements are essentially the same as they are in Java and JavaScript so they are likely to look very familiar.  They aren't all that much different than they are in Visual Basic.  The most basic code block is for a single if statement

if (condition)
{

//  block of code to execute

}

Operators.  When developing the conditions it is usually necessary to make use of some sort of comparison operator.  The following table surveys the comparison operators.

 

Comparison Operators
Symbol Description
= = is it equal to?
!= is it not equal to?
< is it less than?
> is it greater than?
<= is it less than or equal to?
>= is it greater than or equal to?

 

The next table gives a quick reference to the operators that are usually used to create more complicated conditions.

 

Logical Operators
Symbol Description
! is not
&& and
| | or

 

Using these you can form more sophisticated conditions such as

((x < $user_input) && (counter < $upper_limit))

Notice how the entire condition is within a set of parentheses.  This is required in order to delimit the condition.

A Simple Example.  The following script gets the current time and tells the viewer whether or not it is morning using if (condition) blocks.  The condition is makes use of parsing the date like we did in a previous webpage.  The file should be called morning_or_not.php.

 

<html>
<head>
<title>Good Morning?</title>
</head>

<body bgcolor = "000056" text = "cccccc">
<?php
// obtaining the current date and parsing it
$date_time = date("r");
print "The current date - time is <b>$date_time </b><BR>\n";
$hour = date("G");
print "The hour is <b>$hour </b><BR>\n";
if ($hour < 12)
{

print "<font size = 4><b>Good Morning!</b></font><BR>\n";

}
if ($hour >= 12)
{

print "<font size = 4><b>Good Not Morning!</b></font><BR>\n";

}
?>
</body>
</html>

 

But you can see the slight awkwardness in the use of two if (condition) blocks.  This helps motivate the next construct where

if (condition)
{

//  block of code to execute

}
else
{

//  another block of code to execute

}

Thus we only have to evaluate one condition and can choose which block to go through based on the evaluation.  The previous code is modified to make use of this structure in morning_or_else.php.

 

<html>
<head>
<title>Good Morning?</title>
</head>

<body bgcolor = "000056" text = "cccccc">
<?php
// obtaining the current date and parsing it
$date_time = date("r");
print "The current date - time is <b>$date_time </b><BR>\n";
$hour = date("G");
print "The hour is <b>$hour </b><BR>\n";
if ($hour < 12)
{

print "<font size = 4><b>Good Morning!</b></font><BR>\n";

}
else
{

print "<font size = 4><b>Good Not Morning!</b></font><BR>\n";

}
?>
</body>
</html>

 

This is an obvious improvement and it presents a structure we are going to see over and over again.  But our program still has its flaws in terms of how accurately it determines whether or not it is morning.

Now we will get a bit more precise in our evaluation of whether or not it is morning.  This will be done by using a compound condition to determine whether or not it is morning.  You should call this file morning_precise.php.

 

<html>
<head>
<title>Good Morning?</title>
</head>

<body bgcolor = "000056" text = "cccccc">
<?php
// obtaining the current date and parsing it
$date_time = date("r");
print "The current date - time is <b>$date_time </b><BR>\n";
$hour = date("G");
print "The hour is <b>$hour </b><BR>\n";
if (($hour < 12) && ($hour > 5))
{

print "<font size = 4><b>Good Morning!</b></font><BR>\n";

}
else
{

print "<font size = 4><b>Good Not Morning!</b></font><BR>\n";

}
?>
</body>
</html>

 

At least we are no longer telling people good morning when it is just after midnight!

Finally, we will make use of a structure that allows for more conditions to be evaluated in a sort of cascading fashion.

if (condition_1)
{

//  block of code to execute

}
elseif (condition_2)
{

//  another block of code to execute

}

. . .

elseif (condition_n)
{

//  another block of code to execute

}
else
{

//  another block of code to execute

}

Now we have conditions that are linked to each other, each one being evaluated only if the condition just previous to it fails to be satisfied.  The final else catches everything that gets to it.

Now we will modify the program to do more things like tell us whether it is morning, afternoon, evening and so on.  You should call this file morning_etc.php.

 

<html>
<head>
<title>Good Morning?</title>
</head>

<body bgcolor = "000056" text = "cccccc">
<?php
// obtaining the current date and parsing it
$date_time = date("r");
print "The current date - time is <b>$date_time </b><BR><BR>\n";
$hour = date("G");
print "The hour is <b>$hour </b><BR>\n";
if (($hour < 12) && ($hour > 5))
{

print "<font size = 4><b>Good Morning!</b></font><BR>\n";

}
elseif (($hour >= 12) && ($hour < 17))
{

print "<font size = 4><b>Good Afternoon!</b></font><BR>\n";

}
elseif (($hour >= 17) && ($hour < 19))
{

print "<font size = 4><b>Good Evening!</b></font><BR>\n";

}
elseif (($hour >= 19) && ($hour < 23))
{

print "<font size = 4><b>Good Night!</b></font><BR>\n";

}
else
{

print "<font size = 4><b>You Should Probably be Sleeping!</b></font><BR>\n";

}
?>
</body>
</html>