Microsoft Word - Sam's Teach Yourself MySQL in 21 Days - SAMS.doc

(singke) #1

Enter PHP


CGIs can be written in a number of languages. On typical UNIX-based systems, they are written in Perl, but
C, C++, and other languages can be used. Newer languages include Microsoft's ASP, Sun's JSP, and
NetObjects, a fourth-generation solution to server-side programming. PHP has emerged from the open
source camp.


PHP scripts are embedded within HTML files. When a PHP-enabled Web server receives a request for
an HTML file containing PHP, it does not simply give the file to the user—it first executes the PHP
scripts contained within the file.

The script may result in HTML which is generated on-the-fly. But, at a more complex level, it may
include instructions to update files and data stored on the server, even send email and generate
graphics on-the-fly.

The result is totally transparent to users; they need not know that the page has been generated
especially for them! They never get to see the PHP script and may not even realize it is there.
PHP, unlike other traditional CGIs, such as Perl, has the convenience of being contained within HTML
pages; It does not have to reside in a cgi-bin directory. It also has a well-developed library of
functions and APIs that enable it to interface with all manner of server functionality.

As of this writing, PHP is at version 3, but version 4 is in beta and will be released soon. This
introductory chapter intentionally avoids going into a close principles.

Installing and Running PHP


Here's the good news: there are versions of PHP that will run on the various versions of Microsoft Windows,
on UNIX and Linux, and for the most popular Web servers, including Apache and IIS. It's also free, whether
it's used for personal, educational, or commercial use.
Space in this book does not permit a complete guide to installing and setting up PHP. However, it is
freely downloadable from the official PHP Web site (http://www.php.net/) where you will find a full
guide to installation on your particular platform.
Another useful site is http://www.phpbuilder.com/, with lots of resources and discussion archives
for PHP developers.


PHP Basics


Consider a simple example of a Web page containing some PHP code:


<HTML>
<HEAD>
<TITLE>My first PHP page</TITLE>
</HEAD>
<BODY>
<?php
echo "The time is now ".time();
?>
</BODY>
</HTML>
You would save this file on your Web server as showtime.php3. (Note that you have used a .php3
extension. Depending on the PHP setup in the Web server's configuration file on your system, you may
be able to use .php or even .html.)

When you visit the page with a Web browser, you should get an output along the lines of
The time is now 951164380
Note If your browser displays the HTML and PHP code at this point, you may have a
configuration problem on the server. It may be as simple as the filename
extension needing to be changed. Try changing the extension to .php. This
Free download pdf