PHP 101-Part2

Reviews
Shared by: Guillaume
Tags
Stats
views:
355
rating:
not rated
reviews:
0
posted:
11/15/2007
language:
pages:
0
http://devzone.zend.com/node/view/id/627 - Edited from the web PHP 101 (part 2): Calling All Operators by Vikram Vaswani Not What You Expected Form... ...And Function Operating With Extreme Caution A Question of Logic Older But Not Wiser If Not This, Then What? Spreading Confusion The Daily Special Not What You Expected 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: #1 Enter your message: The critical line in this page is the
tag : ...
As you probably already know, the "action" attribute of the
tag specifies the name of the server-side script (message.php in this case) that will process the information entered into the form. The "method" attribute specifies how the information will be passed. ...And Function Now for the other half of the puzzle: the message.php script. This script reads the data submitted by the user and "does something with it". Here is message.php: 1 http://devzone.zend.com/node/view/id/627 - Edited from the web $input"; ?> When you enter some data into form.htm (let's say "Boo"), and submit it, the form processor message.php will read it and display it to you ("You said: Boo"). Thus, whenever a form is submitted to a PHP script, all variable-value pairs within that form automatically become available for use within the script, through a special PHP container variable: $_POST. You can then access the value of the form variable by using its "name" inside the $_POST container, as I did in the script above. Obviously, PHP also supports the GET method of form submission. All you need to do is change the "method" attribute to "get", and retrieve values from $_GET instead of $_POST. The $_GET and $_POST variables are actually a special type of PHP animal called an array, which I'll be teaching you about shortly. Don't worry too much about it at the moment, just make sure you're comfortable with retrieving simple values from a form with PHP, and then scroll on down to learn about some more operators that are useful in this context. Operating With Extreme Caution Thus far, the scripts we've discussed have been pretty dumb. All they've done is add numbers and strings, and read back to you the data you typed in yourself - not exactly overwhelming. In order to add some intelligence to your scripts, you need to know how to construct what geeks call a "conditional statement" - a statement which lets your script perform one of a series of possible actions based on the result of a comparison test. And since the basis of a conditional statement is comparison, you first need to know how to compare two variables and determine whether they're identical or different. You've already seen some of PHP's arithmetic and string operators. However, the language also comes with operators designed specifically to compare two values: the so-called "comparison operators". Here's an example that demonstrates them in action: #2 ”; $result = ($mean < $median); print "result is $result
"; // greater-than operator returns true if left side is greater than right // returns false here $result = ($mean > $median); print "result is $result
"; //continued…. 2 http://devzone.zend.com/node/view/id/627 - Edited from the web // less-than-or-equal-to operator returns true // if left side is less than or equal to right // returns false here $result = ($median <= $mode); print "result is $result
"; // greater-than-or-equal-to operator // returns true if left side is greater than or equal to right // returns true here $result = ($median >= $mode); print "result is $result
"; // equality operator returns true if left side is equal to right // returns true here $result = ($mean == $mode); print "result is $result
"; // not-equal-to operator returns true if left side is not equal to right // returns false here $result = ($mean != $mode); print "result is $result
"; // inequality operator returns true if left side is not equal to right // returns false here $result = ($mean <> $mode); print "result is $result"; ?> The result of a comparison test is always Boolean: either true (1) or false (0 - which doesn't print anything). This makes comparison operators an indispensable part of your toolkit, as you can use them in combination with a conditional statement to send a script down any of its multiple action paths. PHP 4.0 also introduced a new comparison operator, which allows you to test both for equality and type: the === operator. The following example demonstrates it: #3 "; /* returns false, since the variables are not of the same type even though they have the same value */ $result = ($str === $int); print "result is $result
"; //actually prints nothing! We’ll fix this /* returns true, since the variables are the same type and value */ $anotherInt = 10; $result = ($anotherInt === $int); print "result is $result"; ?> Read more about PHP's comparison operators at http://www.php.net/manual/en/language.operators.comparison.php. 3 http://devzone.zend.com/node/view/id/627 - Edited from the web A Question of Logic In addition to the comparison operators I used so liberally above, PHP also provides four logical operators, which are designed to group conditional expressions together. These four operators logical AND, logical OR, logical XOR and logical NOT - are illustrated in the following example: #4 "; /* logical OR (||) returns true if any condition is true */ // returns true $result = (($status == 1) || ($role <= 2)); print "result is $result
"; /* logical NOT (!) returns true if the condition is false and vice-versa */ // returns false $result = !($status == 1); print "result is $result
"; /* logical XOR (xor) returns true if either of two conditions are true, or returns false if both conditions are true */ // returns false $result = (($status == 1) xor ($auth == 1)); print "result is $result
"; ?> Logical operators play an important role in building conditional statements, as they can be used to link together related conditions simply and elegantly. View more examples of how they can be used at http://www.php.net/manual/en/language.operators.logical.php. Older But Not Wiser Now that you've learnt all about comparison and logical operators, I can teach you about conditional statements. As noted earlier, a conditional statement allows you to test whether a specific condition is true or false, and perform different actions on the basis of the result. In PHP, the simplest form of conditional statement is the if() statement, which looks something like this: if (condition) { do this! } The argument to if()is a conditional expression, which evaluates to either true or false. If the statement evaluates to true, all PHP code within the curly braces is executed; if it does not, the code within the curly braces is skipped and the lines following the if() construct are executed. See how the if() works by combining it with a form. Here the user is asked to enter his or her age. Enter your age:
#5 4 http://devzone.zend.com/node/view/id/627 - Edited from the web If 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 < 21) { echo "You're too young for this club, come back when you're a little older"; } ?> 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. #6 = 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 older"; } ?> 5 http://devzone.zend.com/node/view/id/627 - Edited from the web 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 singlestatement if-else block. So, while you could do this: 10) { $msg = 'Blocking your account...'; } else { $msg = 'Welcome!'; } ?> #7 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. 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! } 6 http://devzone.zend.com/node/view/id/627 - Edited from the web And here's an example that demonstrates how to use it: #8

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. 7 http://devzone.zend.com/node/view/id/627 - Edited from the web 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. Practice exercises: For each of the eight numbered exercises within this document, please do the following: Copy/paste the code, save it as a php file and ftp it csmaster. Although many of the examples are presented as php code that is embedded into complete html documents, some are not, so you will have to create the html environment for those. Once you have created the file, open it within your favorite web browser, and be sure you understand what is going on. Do this with all of the examples in this document. Please note that there are homework exercises associated with some of these practice problems, so look below before you leave a particular problem. Homework on this material – due Wed., Sept. 5 – post your files in your private folder on csmaster in a folder called PHP101-Part2. 1. Using Programs #1 (form and action files together), create a form that requests a user’s name in three separate fields: first name, middle initial and last name. Produce the form to display the user’s entire name on one line. 2. Amend Program #2 to produce the words “true” or “false” instead of 0 (or nothing) and 1. You may want to look ahead to the conditional section for this. 3. Create a form in which the user is instructed to enter 3 integers, and then process it to display the integers in ascending order. 4. Create a form in which the user is instructed to enter an integer, and then process it to display whether the integer is even or odd – use the % operator. Example: 6 % 3 is 0 because 6 / 3 has no remainder (i.e., a remainder of 0) but 6 % 4 is 2 because the remainder of the operation 6 / 4 has a remainder of 2. 5. To be a leap year, the year must be evenly divisible by 4. However, not all years that are evenly divisible by 4 are leap years: Specifically, years that are divisible by 100 but are not divisible by 400 are NOT leap years. E.g., 1996 and 2000 are leap years, but 1998 and 1900 are not. Create a form for the user to enter a year, and then process it to determine and display to the user whether or not they have entered a leap year. 6. Amend the files for Problem #8 to handle each day of the week with a different special. Use a switch statement. (see Part 3 – Problem #1) 8

Shared by: Guillaume
Other docs by Guillaume
YouTube-039-s-Official-Authorities-The-Users-70079
Views: 1681  |  Downloads: 12
YouTube-Fights-Against-Its-Father-Google-55082
Views: 1409  |  Downloads: 11
xna_launch_final_report
Views: 1381  |  Downloads: 5
XNA_Introduction
Views: 1106  |  Downloads: 11
xna
Views: 1036  |  Downloads: 4
XNA Development-1
Views: 1864  |  Downloads: 10
xmas_05
Views: 981  |  Downloads: 0
xerc_users_manual
Views: 1090  |  Downloads: 1
xbst
Views: 1030  |  Downloads: 0
Xbox Way
Views: 1099  |  Downloads: 0
XboxVGA Video Setup
Views: 559  |  Downloads: 0
xbox-router
Views: 377  |  Downloads: 0
xboxnext_security
Views: 251  |  Downloads: 2
XBoxMACAddress
Views: 919  |  Downloads: 0
Related docs
PHP
Views: 1176  |  Downloads: 153
about php
Views: 11  |  Downloads: 1
Session in PHP
Views: 44  |  Downloads: 6
PHP AND AJAX
Views: 508  |  Downloads: 143
php
Views: 45  |  Downloads: 3
Tutorial PHP
Views: 107  |  Downloads: 18
php
Views: 835  |  Downloads: 19