Web Design:
|
|
|
|
|
Richard H. Nilsson, January 10, 2005 Why PHP?PHP is a server-side scripting language similar in syntax and grammer to JavaScript, but with many strong advantages over its client-side predecessor. Like JavaScript, PHP is free, in the sense that all you have to do is host your site with an ISP that provides it. PHP is provided free on some hosting services with even their basic services in the under-US$20/month range. I prefer the combination of Linux OS, Apache Web server, MySQL and PHP for my hosting choice, commonly referred to as "LAMP" technology. Unlike JavaScript, PHP code is invisible to the browser. Prior to sending the page, the server strips your PHP code, interprets it, and uses it to control the HTML that finally reaches the browser. With PHP you can maintain files, manipulate images, and update database contents on the server so your Web pages can interact dynamically with visitors. In the Apache environment, PHP responds faster then Java, another active-site development language. Using PHP allows quick development times. PHP programmers are more ubiquitous and generally less expensive for small projects than Java-bean jugglers. PrerequisitesThis tutorial assumes you have access to a Web directory on a local or ISP host, and you have access to PHP on that server. One reasonably-priced ISP that provides this environment with outstanding and easy to use tools, is ApolloHosting.com . PHP Page StructureLet's try a simple PHP page. PHP code is detected by the Web server as all code between the symbols "<?php" and "?>". Lines of code and these tags surrounding them are what I call a code "block". Depending on how the Web server is configured, the opening tag can sometimes be simplified to "<?", but for portability, I always open code blocks with the full "<?php". PHP code blocks may appear anywhere on the page interspersed with the HTML. It is general practice to place functions (subroutines used many places in the page by other code) at the bottom of the page, and group the code that modifies page appearance near the HTML it changes. Apache detects pages it should route to the PHP interpreter by the page file extention ".php". Type the following listing into a text editor and save it as php_test.php in your site's "public_html" or Web root directory, then access the page:
Can you identify the two "code blocks"? The first few lines create a PHP variable denoted by the word starting with "$" ("$page_name") and set its value to the text string between double quotes. Below, the HTML <title> tag is supplied this value. The "echo" statement functions much like a "print" statement. To find out the differences, and to get more details on other functions we will be demonstrating, see the on-line PHP Function Reference. I keep a link to this handy page in my browser's toolbar. Try replacing the code in the <body> with the following snippet and see what happens (after the "!" sign, press return so "CURTIME()" starts on a new line as shown):
... and here's another way, similar to how you'd print values retrieved from a database:
Looks the same, doesn't it? Notice how the variables substitute in for the "%s" placeholders. To control flow in a program, we use variations of "if" statements:
Here, the test condition is evaluated and if the evaluation produces a positive (0 or higher) result, the code between the braces ("{ }") is executed. The double-slash indicates a comment line. Nothing on a comment line will be executed. Test conditions can be whether a variable is present or not ["if ($variable)"] or if the variable equals a certain string or quantity ["if ($variable == 'Rik')" or "if ($age <= 30) { $category = 'young'; }"] or if the variable is NOT equal or present ["if (!$sexy) { $ignore = 1; }" or "if ($variable != 5)"]. Here's a variation that provides an alternative block of code to execute if the test-condition fails:
What's also interesting about the above is that the paragraph of italic HTML code will not appear in the page if the test-condition is true! It is isolated from the browser until the test-condition fails because it is between braces. Notice the closing brace for the "else", in a re-opened PHP tag block. So far, you've been able to generate text content on an HTML page, and used a variable to supply the page's title. OK, now let's get into the meat of PHP. In the process, we'll use other aspects of this powerful language. The ProjectA contact form handler requires the use of several major capabilities of PHP and makes a good choice for a tutorial. Basic features of our example contact form handler are:
Set up the logOur contact page emails the form data to a specified recipient. It also stuffs the form data into a plaintext file. If you suspect your mail is not working, or get errors when the mail server is down, you have the fall-back of having saved the submitted info in an easily readable text file. All you need to do to prepare for this logging function is create a forms_log directory adjacent to this script page. Do that now. Log in to your site using ssh, change to the public_html directory if you are not there, and execute the make-directory command while in your public_html directory: $ cd public_html // if necessary $ mkdir forms_log $ chmod 644 forms_log Unless you got an error, you now have a directory into which our script will save form submissions. Page 1 Page 2 >> Copyright 2005 Richard Nilsson. Verbatim copying and redistribution of this entire article are permitted without royalty in any medium provided this notice is preserved. |
|