PHP Basics WorkshopDeveloped by: Paul Burney
IntroductionPHP is short for PHP Hypertext Preprocessor. What it does is to process hypertext (i.e., HTML web pages) before they leave the web server. This allows you to add dynamic content to pages while at the same time making that content available to users with all types of browsers. PHP is an interpreted programming language, like Perl. With PHP you can do almost anything. Really. You can connect to any thing that you would want to on the command line, create interactive pages, PDF files, and images, and connect to database, LDAP, and email servers. This workshop will serve as an introduction to the most basic elements of PHP and how to use them to create dynamic pages. To participate in this workshop you need:
If you don't have one or more of these programs installed on your computer, much of it can be downloaded for free, at least for a limited demonstration period. Check the vendor's web site for more information, or perform a search on a site such as Download.com. If you would like, you can print this entire workshop. Language SyntaxTo add PHP code to a web page, you need to enclose it in one of the following special sets of tags:
So, what kind of code goes where it says php_code_here? Here's a quick example.
If you copy that code to a text editor and then view it from a web site that has PHP enabled you
get a page that says Hi There. The A little more information can gained by using the PHP info command:
This page will display a bunch of information about the current PHP setup on the server as well as tell you about the many built in variables that are available. Please note: Most server configurations require that your files be named with a .php3 extension in order for them to be parsed. Name all of your PHP coded files filename.php3. In the next section, we will discuss using your own variables. VariablesTo declare a variable in PHP, just place a $ character before an alpha-numeric string, type an equals sign after it, and then a value.
The above code sets the variable $greeting to a value of "Hello World". We can now use that variable to replace text throughout the page, as in the example below:
The above code creates a page that prints the words "Hello World". One reason to use variables is that you can set up a page that repeats a value throughout and then only need to change the variable value to make all the values on the page change. In the next section, you will see how to put variables to work. OperationsNow we'll take a look at performing some operations on some variables. Most scripts do nothing more complicated than this. To examine these ideas, we'll create an application that calculates a tip for a bill of a given price. First, we'll create the html form for the user to fill in. You can use any editor to do this. Here's the source:
You can see the actual page at tips.html. Let's look at a few of the highlights in this page. The first is the action of this page, tips.php3. That means that the web server is going to send the information contained in this form to a page on the server called tips.php3 which is in the same folder as the form. The names of the input items are also important. PHP will automatically create a variable with that name and set its value equal to the value that is sent. Now we need to create a PHP page that will handle the data. Of course, this page needs to be named tips.php3. The source is listed below. One way, perhaps the best way, to create a PHP page is to create the results page in a graphical editor, highlighting areas where dynamic content should go. You can then use a text editor to replace the highlighted area with PHP.
We'll look at this code a bit more closely, paying most attention to the line numbers that are highlighted. Note that the line numbers are there for illustrative purposes only. Please do not include them in your source code. Lines 11 and 13 check to see if the $sub_total and $tip_percent variables are empty. If they are, they give an error message. Line 15 converts the tip percentage into a decimal that we can multiply by. Line 17 multiplies the tip decimal by the sub total to get the tip. Line 19 gets the total cost by adding the sub total to the tip. Lines 25, 27, 29 and 31 display the PHP variables on the results page. You can now see how the page works at tips.html. Arrays and LoopsAn array is a group of several related variables. A loop is typically used to go through an array and do something with the values. To explore this topic, we will setup a small text submission form, sort the output, and do some checking on the data to see whether or not particular words are present. First, we'll create the html form for the user to fill in. You can use any editor to do this. Here's the source:
You can see the actual page at text.html. Let's look at a few of the highlights in this page. The first is the action of this page, text.php3. That means that the web server is going to send the information contained in this form to a page on the server called text.php3 which is in the same folder as the form. The names of the input item is also important. PHP will automatically create a variable with that name and set its value equal to the value that is sent. Now we need to create a PHP page that will handle the data. Of course, this page needs to be named text.php3. The page will take in the data, look at it, and then send it back to the user after sorting and checking it. The source is listed below:
Line 15 takes the variable $text that was sent and breaks it up wherever there is a space and places those pieces into an array called $keywords. Line 17 sorts the $keywords alphabetically. Please note: it does so in typical unix fashion, that is, first upper case, and then lower case. Line 19 counts how many keywords are in the array. Lines 21 begins a loop that will perform code on every element of the array. Lines 23 checks to see if the keyword is dirty or bad. The == operator means "is equal to" (the = operator is used only for assignment of variables) and the || operator means "or". We use parentheses to make the comparisons explicit. If the keyword is bad or dirty, the echo command on line 25 will be sent to the browser. If it isn't, the else part is invoked, line 27, and the echo command on line 29 is invoked instead. You can now see how the page works at text.html. More InfoThe topics covered in this workshop are only the most basic. We've barely scratched the surface of PHP. For more information, check out the following resources. For more info on the features available on the GSE&IS Web Server, check out the GSE&IS About Pages. http://www.gseis.ucla.edu/about/ Generally, if you see anything on any page of the site that you would like to use in your area of the site, you can contact me and we'll try to work it out. Your main reference site for PHP is the main php site and it's online manual: Links to other good php sites, such as devshed, phpbuilder, and webmonkey can be found on the PHP site at: You may also wish to check out the programming section of my favorites file which is posted at: http://www.gseis.ucla.edu/etu/training/materials/web-links.html For more information on HTML tags, check out the ZD Net Tag Library at: http://www.gseis.ucla.edu/about/ For questions, feel free to call or email me, or to ask questions on the ask the webmaster forum, join the php e-mail list, or the UCLA Campus web publishers e-mail list. |
|||||