Please note:
This material is adapted from Build Your Own Database Driven Website Using PHP and MySQL, by Kevin Yank. 2nd Edition, published by Sitepoint Pty. Ltd. ISBN 0-9579218-1-0. The book includes much more information, and you are encouraged to buy it!
Chapter 3 Notes, page 3
Control Structures
PHP includes statements that allow us to affect the "flow of control" in a script. The most basic one is the if-else statement. For example:
if (condition) {
//statement to be executed if the condition is true
}
else {
//something else (optional) to be done if the statement is false.
}
Well, here is a more specific example. Let's look at control1.php, accessed by control1.html.
Another frequently used control structure is the while loop. This allows us to use a condition to decide how many times to execute a set of statements. Here is what a "while loop" looks like:
while (condition) {
//This statement
//That statement
//and/or the other statement(s)
//that will be executed as long as the condition is true
}
This is similar to the if-else statement without an else clause. The difference arises when the statement is true, and the statement is executed. Instead of continuing the execution after the closing brace, the condition is checked again. If it is still true, the statements are executed again until the statement becomes false. At that point, execution jumps tot the next statement following the while loop, after the closing brace. An example: counter1.php. [For local demo: Local counter1.php.
We can also use a for loop. Looks like so:
for(initialize; condition; update) {
//Statements to execute as long as the condition remains true after each update
}
This "for loop" initializes and increments the counter variable on the same line as the condition. It is often easier to have all the stuff that controls the loop in the same place. This is what counter2.php looks like.
The Multipurpose Page Exercise
This exercise creates a mini-site with pages configured as follows: We want to show the viewer's name on every page, and we don't know which page he/she will visit first. So the pages have to "know" whether or not a name has been provided, and if so, display the page. If the name has not been provided, the site must ask for the user's name, and display the page after it has been provided.
Assuming we can get the name, (which we discuss shortly), it can be passed, along with a request for another page, by adding the name to the query string of all links:
<a href="newpage.php?name=<?php echo(urlencode($_GET['name']));?>">
Link to next page</a>
Holy cow! What is all that stuff?? Not too bad, really.
- First, it's a link to the next page - a PHP page - that passes the value of 'name' (never mind where it gets that from for now), so it starts with an href to the new page, and includes the "name=" that we saw back in welcome1.html.
- Ok, urlencode. This is a function. It takes any "special characters" - such as spaces - in the string, and converts them to the codes they need to be in order to appear in the query string.
- Suppose the $name variable had a value of "Kevin Yank." Since spaces are not allowed in the query string, the output of "urlencode" would be "Kevin+Yank." When PHP created the $_GET variable in newpage.php, it would convert "Kevin+Yank" back to "Kevin Yank."
- So, instead of having just "$_GET['name']" in the query string, we are using urlencode to clean up the spaces (in this case.)
- So how do we get the name in the first place? We could put a form, like the ones in the prior examples, on a page, but we don't know that the user will start there. It isn't going to look right to have a form on every page, either. If the user starts with the page with the form, providing a name, the next pages requested have to know that the name has already been provided. On the other hand, if the user starts with a different page, the page has to "know" that no name has been provided yet. We have to construct the pages so that they check to see whether the name has been given and to ask for it (via a form) if not.
- To do this, each page will check to see whether the $name variable has a value or not. It will make use of an if-else statement.
Switching In and Out of PHP
(One nice thing about PHP is that you can "turn it on and off" as needed. Think of <?php as the command to go into "PHP mode," and ?> as the command for going out of "PHP mode." We can see this used in the pages created for this exercise.)
Now, I did not use the "isset" variable used by Kevin Yank in his example on page 66. I wanted to see if I could do without it, and it worked.
Note the use of a new variable, $_SERVER['PHP_SELF'], to specify the action attribute of the <form> tag. Like $_GET, $_POST, and $_REQUEST, $_SERVER is an array variable created by PHP. It contains a lot of information supplied by your web server. Specifically, $_SERVER['PHP-SELF'] will always be set to the URL of the current page. We are using this to create a form that, when submitted, will load the very same page, but this time with the $name variable specified, which is what we needed to do. So let's look at newpage1.php and its little friends, newpage2.php and newpage3.php. Start here. Or, for a local demo, start here.