If you want to make your Web pages do significant and interesting things with PHP, you will eventually need to let the PHP code make some decisions about the information, events, and/or data that it is dealing with. This handout will teach you how to deal with different values and conditions in the page.
Before we talk about conditions, though, we will first look at a handy way to get some information from your Web page's user. I am referring to forms.
We will see forms again later in the semester. For now, this section describes a simple way to get information from the user of your page, and to display submitted form data in another page, usually called the action page. (The term "action page" refers to the page which receives submitted form data, because the <form> tag has an action attribute which tells the browser where to send the submitted data.)
First, here is a simple HTML form: This is page testFormData2.html.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="myStyles.css">
<title>Display Form Data</title>
</head>
<body>
<div id="mainDiv">
<form name="theForm" action="displayFormInfo2.php"
method="post">
Please enter your name:
<input type="text" name="theName" id="theName" />
<br />
<br />
Please enter your phone number:
<input type="text" name="theNumber" id="theNumber" />
<br />
<br />
<input type="submit" value="Send the form information" />
</form>
</div>
</body>
</html>
Secondly, here is the PHP action page that displays the form information: This is page displayFormInfo2.php.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="myStyles.css">
<title>Display Form Info</title>
</head>
<body>
<div id="mainDiv">
<h3>This is the form data that was sent:</h3>
<?php
print "The Name: ";
print $_POST["theName"];
print "<br />";
print "The Number: ";
print $_POST["theNumber"];
?>
</div>
</body>
</html>
You can see the sample pages running here.
Please note these points about the above code:
$_POST["theName"]
In this section, we will use an HTML form page that is almost identical to the HTML form page in previous section, but we will see another way to display form information in a PHP page.
First, here is the HTML form page: This is page testFormData3.html.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="myStyles.css">
<title>Display Form Data</title>
</head>
<body>
<div id="mainDiv">
<form name="theForm" action="displayFormInfo3.php"
method="post">
Please enter your name:
<input type="text" name="theName" id="theName" />
<br />
<br />
Please enter your phone number:
<input type="text" name="theNumber" id="theNumber" />
<br />
<br />
<input type="submit" value="Send the form information" />
</form>
</div>
</body>
</html>
The PHP action page is displayFormInfo3.php:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="myStyles.css">
<title>Display Form Info</title>
</head>
<body>
<div id="mainDiv">
<h3>A Form Letter:</h3>
<?php
$theNumber = $_POST["theNumber"];
print <<<HERE
Thank you, {$_POST["theName"]}, for your request.
<br />
<br />
We will contact you at phone number $theNumber
within 3 business days.
<br />
<br />
Sincerely,
<br />
<br />
The Management
HERE;
?>
</div>
</body>
</html>
You can see the sample pages running here.
Please note these points about the above code:
$_POST["theName"]is inserted directly into the string with code like this:
Thank you, {$_POST["theName"]}, for your request.
Note the curly braces around the built-in $_POST variable.
The simplest way for you to instruct a PHP page to make a decision, is to use the if statement.
For the next several handout sections, we will use the same input form as we used at the beginning of this handout. But for the action pages we will use several different pages which illustrate the conditions that we are discussing.
The Condition Form 1 Page demonstrates the use of an if statement.
The code for the action page is shown below. This is page conditionAction1.php:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="myStyles.css">
<title>Simple IF Statement</title>
</head>
<body>
<div id="mainDiv">
<h3>A Form Letter:</h3>
<?php
$theName = $_POST["theName"];
$theNumber = $_POST["theNumber"];
if ($theName == 'Jim')
{
echo "<p>Welcome back, Mr. Link!</p>\n";
}
echo "<p>Your number is " . $theNumber . ".</p>\n";
?>
</div>
</body>
</html>
Please note these points about the above action page:
That last point is extremely important. The following two PHP statements are both correct syntactically, but they do very different things:
$x = 5;assigns the value 5 to the variable $x, using the assignment operator, which is =; and
$x == 5tests to see if the value 5 is in the variable $x, using the comparison operator for equality, which is ==.
There are other types of comparison that you can do in PHP. The following table shows the comparison operators that you can use in conditional statements:
| Operator | Description |
|---|---|
| == | Equal to |
| < | Less than |
| > | Greater than |
| <= | Less than or equal to |
| >= | Greater than or equal to |
| != | Not equal to |
You can also code your page so that it does something if the condition that you are testing for is FALSE.
In PHP, one way that you tell the page to do something if the condition is FALSE, is to use the else clause.
The Condition Form 2 Page demonstrates the use of an else clause.
The code for the action page is shown below. This is page conditionAction2.php:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="myStyles.css">
<title>Simple IF and ELSE Statement</title>
</head>
<body>
<div id="mainDiv">
<h3>A Form Letter:</h3>
<?php
$theName = $_POST["theName"];
$theNumber = $_POST["theNumber"];
if ($theName == 'Jim')
{
echo "<p>Welcome back, Mr. Link!</p>\n";
}
else
{
echo "<p>Welcome, honored guest!</p>\n";
}
echo "<p>Your number is " . $theNumber . ".</p>\n";
?>
</div>
</body>
</html>
Please note these points about the above action page:
Sometimes you need to combine conditions in the conditional expression of an if statement.
The following table shows the logical operators that you can use in combining conditional statements:
| Logical Operator | Description |
|---|---|
| && | And - returns true if both the left operand and right operand return a value of true; otherwise, it returns false |
| || | Or - returns true if either the left operand or right operand returns a value of true. If both the left and right operands return a value of false, this operator returns false. |
| ! | Not - returns true if an expression is false, and returns false if an expression is true |
The && and || operators are binary operators, which means that there are two things (technically called "operands") to do something with.
The ! operator is a unary operator, which means that there is only one thing (technically called the "operand") to do something with.
The following two tables should help you understand under what conditions the logical operators return true or false:
| If Condition 1 is | and Condition 2 is | the result of Condition 1 AND Condition 2 is |
|---|---|---|
| true | true | true |
| true | false | false |
| false | true | false |
| false | false | false |
| If Condition 1 is | and Condition 2 is | the result of Condition 1 OR Condition 2 is |
|---|---|---|
| true | true | true |
| true | false | true |
| false | true | true |
| false | false | false |
Here is an example of how you can combine conditions. This is file logicalOperators.php:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="myStyles.css">
<title>Logical Operators</title>
</head>
<body>
<div id="mainDiv">
<h3>Logical Operators</h3>
<?php
$a = 5;
$b = 7;
$c = FALSE;
if ($a > 1 && $b < 10) // If $a is greater than 1 AND $b is less than 10...
{
echo "The AND condition succeeded.<br>\n";
}
if ($a > 10 || $b < 10) // If $a is greater than 10 OR $b is less than 10...
{
echo "The OR condition succeeded.<br>\n";
}
if (!$c) // If NOT $c... which is the same as If $c is NOT TRUE...
{
echo "The NOT condition succeeded.<br>\n";
}
?>
</div>
</body>
</html>
There are many ways to prevent errors such as "undefined index" when you include an HTML form and its action code in the same page. The code sample given in this section is just one way to prevent such errors.
Here is a sample page which has an HTML form and the PHP action code in the same page. This is page singlePageForm.php, which you can see running here.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="myStyles.css">
<title>Single-Page Form</title>
</head>
<body>
<div id="mainDiv">
<div id="formDiv">
<?php
if (!isset($_POST["formSubmitted"]))
{
?>
<form name="theForm" action="singlePageForm.php" method="post">
<input type="hidden" name="formSubmitted" value="1">
<h3>Type in a color:</h3>
<input type="text" name="color">
<h3>Type in an animal name:</h3>
<input type="text" name="animal">
<h3>Type in country name:</h3>
<input type="text" name="country">
<br>
<br>
<input type="submit" value="Tell a Story">
</form>
<?php
}
else
{
echo "<p>\n";
echo "The {$_POST['color']} {$_POST['animal']} took a nap in {$_POST['country']}.";
echo "<p>Please <a href='singlePageForm.php'>go back</a> and tell another story.</p>\n";
}
?>
</div>
</div>
</body>
</html>
Please note these points about the above code:
You can use multiple else clauses in a series of else if statements. (And see the next section for an even better way to handle multiple values.)
The Condition Form 3 Page demonstrates the use of multiple else if clauses.
The code for the action page is shown below. This is page conditionAction3.php:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="myStyles.css">
<title>Multiple IF ELSE Statements</title>
</head>
<body>
<div id="mainDiv">
<h3>A Form Letter:</h3>
<?php
$theName = $_POST["theName"];
$theNumber = $_POST["theNumber"];
if ($theName == 'Jim')
{
echo "<p>Welcome back, Mr. Link!</p>\n";
}
else if ($theName == 'Sam')
{
echo "<p>Welcome back, Mr. Johnson!</p>\n";
}
else if ($theName == 'Sarah')
{
echo "<p>Welcome back, Ms. Smith!</p>\n";
}
else
{
echo "<p>Welcome, honored guest!</p>\n";
}
echo "<p>Your number is " . $theNumber . ".</p>\n";
?>
</div>
</body>
</html>
Please note these points about the above action page:
PHP has a decision structure that you can use instead of multiple else if statements. This structure is the switch statement and it is used when you are comparing a variable to many possible values.
The Condition Form 4 Page looks a lot like the previous page, but it uses a switch statement.
The code for the action page is shown below. This is page conditionAction4.php:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="myStyles.css">
<title>The Switch Statement</title>
</head>
<body>
<div id="mainDiv">
<h3>A Form Letter:</h3>
<?php
$theName = $_POST["theName"];
$theNumber = $_POST["theNumber"];
switch ($theName)
{
case 'Jim':
echo "<p>Welcome back, Mr. Link!</p>\n";
break;
case 'Sam':
echo "<p>Welcome back, Mr. Johnson!</p>\n";
break;
case 'Sarah':
echo "<p>Welcome back, Ms. Smith!</p>\n";
break;
default:
echo "<p>Welcome, honored guest!</p>\n";
} // end switch
echo "<p>Your number is " . $theNumber . ".</p>\n";
?>
</div>
</body>
</html>
Please note these points about the above action page: