In Part One of this series, I gave you a brief introduction to PHP, and how it fits into your Web application development environment. I also taught you the basics of PHP variables, and showed you how to add, multiply and concatenate them together.
Now that you know the basics, it's time to focus in on one of PHP's nicer features - its ability to automatically receive user input from a Web form and convert it into PHP variables. If you're used to writing Perl code to retrieve form values in your CGI scripts, PHP's simpler approach is going to make you weep with joy. So get that handkerchief out, and scroll on down.
Form...
Forms have always been one of quickest and easiest ways to add interactivity to your Web site. A form allows you to ask customers if they like your products, casual visitors for comments on your site, and pretty girls for their phone numbers. And PHP can simplify the task of processing the data generated from a Web-based form substantially, as this first example demonstrates. This example contains two scripts, one containing an HTML form (named form.htm) and the other containing the form processing logic (message.php). Here's form.htm:
The critical line in this page is the
As you probably already know, the "action" attribute of the
Depending on whether the entered age is above or below 21, a different message is displayed by the ageist.php script:
= 21) { echo 'Come on in, we have alcohol and music awaiting you!'; } if ($age <>
If Not This, Then What?
In addition to the if() statement, PHP also offers the if-else construct, used to define a block of code that gets executed when the conditional expression in the if() statement evaluates as false.
The if-else construct looks like this:
if (condition) { do this! } else { do this! }
This construct can be used to great effect in the last example: we can combine the two separate if()statements into a single if-else statement.
= 21) { echo 'Come on in, we have alcohol and music awaiting you!'; } else { echo "You're too young for this club, come back when you're a little older"; } ?>
Spreading Confusion
If the thought of confusing people who read your code makes you feel warm and tingly, you're going to love the ternary operator, represented by a question mark (?). This operator, which lets you make your conditional statements almost unintelligible, provides shortcut syntax for creating a single-statement if-else block. So, while you could do this:
10) { $msg = 'Blocking your account...'; } else { $msg = 'Welcome!'; } ?>
You could also do this, which is equivalent (and a lot more fun):
10 ? 'Blocking your account...' : 'Welcome!'; ?>
PHP also lets you "nest" conditional statements inside each other. For example, this is perfectly valid PHP code:
Another, more elegant way to write the above is with a series of logical operators:
The Daily Special
PHP also provides you with a way of handling multiple possibilities: the if-elseif-else construct. A typical if-elseif-else statement block would look like this:
if (first condition is true) { do this! } elseif (second condition is true) { do this! } elseif (third condition is true) { do this! } ... and so on ... else { do this! }
And here's an example that demonstrates how to use it:
Today's Special
As you can see, this is simply a form which allows you to pick a day of the week. The real work is done by the PHP script cooking.php:
Today's special is:
In this case, I've used the if-elseif-else control structure to assign a different menu special to each combination of days. Note that as soon as one of the if() branches within the block is found to be true, PHP will execute the corresponding code, skip the remaining if() statements in the block, and jump immediately to the lines following the entire if-elseif-else block.
And that's it for now. To view more examples of conditional statements in action, visit http://www.php.net/manual/en/language.control-structures.php. In Part Three, I'll be bringing you more control structures, more operators and more strange and wacky scripts - so make sure you don't miss it!
Copyright Melonfire, 2004 (http://www.melonfire.com/). All rights reserved.
No comments:
Post a Comment