An Introduction to PHP3

May 1st, 2000 by John Holland in

If you are desinging a new web site, this language can provide just the help you need.
Your rating: None

One of the latest things to emerge in web development is a language called PHP. According to its proponents at http://www.php.net/, it is in use by Volvo, Red Hat, Miss USA 1999, First USA Bank, NASA JPL and other prominent organizations. One of its most popular applications is connecting to databases, but it can also talk to LDAP, IMAP and SNMP services, generate GIFs on the fly and do other tricks.

PHP3 is a server-side include scripting language and has been compared to Microsoft ASP (Active Server Pages), but with two differences crucial to me: it is open source and it runs on Linux. Actually, it is available under a GNU license or the PHP license, which allows commercial closed-source products to be developed from it. It runs on many UNIX platforms.

What is a server-side include? Think of it as another embedded language, like JavaScript, but which is converted to HTML (or images, JavaScript or other possibilities) on the server. The user's browser doesn't see the PHP code, just the results of executing it. This protects the developer's work, enhances security and bypasses things like conflicting implementations of JavaScript.

For example, the following code:

<HTML><BODY><?PHP echo "Hello, Net" ?></BODY></HTML>

in a PHP page on a PHP-enabled server will appear in the user's browser as Hello, Net.

What can PHP do for you? It greatly reduces the complexity of using cgi-bin variables and connecting to databases, and allows programming in an easy-to-learn language within web pages. It can do much more (the on-line reference lists 53 types of functions), but I will focus on database connectivity.

MySQL seems to be the most popular database to use with it, so I will use that for my examples. PHP can connect to Oracle, Sybase, PostgreSQL, Informix and other databases as well. It is important to note that PHP is not database-independent, unlike the DBI system used with Perl. You must use PHP functions tailored for the database you are targeting. This lets PHP access functionality available only in a specific database platform, but may reduce the portability of database-enabled projects.

In the following discussion, I will assume the reader has a knowledge of SQL, HTML and C.

I've already shown a “Hello, World” in PHP above. Note that the entire PHP code is embedded in a single tag within the HTML. The convention is to assign the extension .php3 to pages that should be pre-processed by PHP, although this is configurable. The page contains HTML (and whatever else the server and browser support, such as JavaScript) with one or more <?php ...php code... ?> blocks. As I mentioned above, the PHP is evaluated and its output sent to the browser, typically as HTML. PHP can also send any text language or even generate images on the fly.

The PHP blocks can be included anywhere in the HTML—even inside quotes or tags. For example, a link to another page could be produced from PHP. The following example:

<HTML><BODY>
...
For more information, visit <A HREF=
"<?php echo "moreinfo.html" ?>"> wacky link </A>
</BODY> </HTML>

would work, although there is little point to it. The value of PHP is that it is a powerful programming language. It allows things like links to be generated by code. One can build a whole interactive web site with it.

A Quick Overview of the Language

PHP is much like basic C, a little like Perl, and has a few unique features of its own. Here are some key facts:

  • Variables are preceded by $ (just $, not @ or anything else).

  • Primitive types include integers, floats, strings, arrays, associative arrays and objects.

  • Type is usually set automatically, not in code, so there is no need to declare variables. They can be declared as static which makes them persist between PHP blocks on the same page.

  • //, /*..*/ and # are all used for comments—sort of a combination of C++, Perl and shell scripting.

  • Statements are terminated by semicolons.

  • Functions can be declared within a block. Their declaration and call resemble C.

  • Forms submitted to a PHP script pass in their variables (this is quite cool). They are accessible by name and also come in an array, which is nice when your calling form is a PHP program that may have unpredictable variable names.

  • The set of control structures and operators is very similar to C, although with some extensions.

  • There is limited support for object types.

Diving In

Listing 1

Listing 1 is two examples of PHP code to show some of its features. The first page accepts two numbers to add a first and last name. The passing of the form variables to be used in the next page happens automatically, which is one of the neatest things about PHP. This doesn't check their type—that they are actually numbers—in order to give an error. It does show the way PHP can be used to send and receive information to the user.

Listing 2

Listing 3

Listing 2 is an example that generates CGI variables on the fly, and the receiving page (Listing 3) deals with any incoming variables generated. If you run this in a PHP-enabled server, you'll see the variables passed in the cgi-call in the location line of the browser. All Listing 3 does is echo the names of the variables, but it demonstrates a technique that would enable a web site to generate a list on the fly and allow the user to make selections—like a shopping cart.

Listing 4

Now let's connect to a MySQL database. The following code would connect as root to a database named “stores” on the local host, with password “tiger”. It executes a query against a parts table, counts the number of rows in the query to check for the actual data, and if it exists, the result is displayed as an HTML table with check boxes to select items. Listing 4 is the PHP code preceded by the structure of the table. Note that knowing the number of rows is handy for determining whether to display the query at all (if there is any information) as well as counting down until no data is left. Each row of data is a row in an HTML table, with the fields walked through by number. The array containing the row from the query can be addressed by number or by field name, so you could also execute:

$partno=$row["partno"]

This can be advantageous during development when field positions in the table or query may be changing more often than names.

The next page of HTML (called in the FORM command) will receive the checked-off selections as CGI variables, which can be retrieved even though their names are unknown, using the technique shown above. It will then insert into another table the items that have been checked off. See Listing 5.

Listing 5

Installation Information

The latest information on installing PHP is available at http://www.php.net/ or in the distribution itself. I have installed it for FreeBSD, Linux and Win32 using their supplied packages. I have installed it only for Apache, although it is supposed to work with other web servers. It is supplied as source code for platforms other than Win32. The installation for Apache requires recompiling Apache to add it in as a module, although if that is not an option, it can be installed as a binary and scripts can be run as cgi-bin scripts.

Conclusion

I've found PHP3 to be a very enjoyable and versatile language for web applications. As of this writing, the next version—which is primarily billed as a performance improvement—is in beta testing. (PHP 4.0 includes the ZEND engine; see their web site for details.)

Resources

John Holland programs intranet applications as a consultant for Bell Atlantic. He lives outside Washington DC with his wife and children. He can be reached at jholland@jbhsoft.linuxbox.com.

__________________________


Special Magazine Offer -- Free Gift with Subscription
Receive a free digital copy of Linux Journal's System Administration Special Edition as well as instant online access to current and past issues. CLICK HERE for offer

Linux Journal: delivering readers the advice and inspiration they need to get the most out of their Linux systems since 1994.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
enigma free mp3's picture

linux

On October 2nd, 2007 enigma free mp3 (not verified) says:

work? the How Linux networking/TCP stack does . Bye.

Post new comment

Please note that comments may not appear immediately, so there is no need to repost your comment.
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <pre> <ul> <ol> <li> <dl> <dt> <dd> <i> <b>
  • Lines and paragraphs break automatically.

More information about formatting options

Newsletter

Each week Linux Journal editors will tell you what's hot in the world of Linux. You will receive late breaking news, technical tips and tricks, and links to in-depth stories featured on www.linuxjournal.com.
Sign up for our Email Newsletter

Featured Videos

Setting up an https server in Apache is easy. This tutorial covers how to create and sign your ssl certificate as well as how to configure the web server.

From the Magazine

January 2009, #177

It's a battle as old as time: good vs. evil. Fortunately, Linux and FOSS are on our side as we wage the battle against those who try to steal our secrets and invade our systems.

Checking your system's security is best done sooner rather than later. Test the locks with our article on security verification; find out how to use PAM to help secure your systems; use MinorFS and AppArmor to implement discretionary access control; learn more about Samba security in part III of our series; use Darknet to help detect bots and secure your systems; use the Yubikey to increase your site's security; and don't forget to lock the doors, because a cold boot attack could render your security useless if somebody has physical access to your computer.

But, we're not just about sowing the seeds of fear. We also show you how to use memcached in Rails, how to manage multiple servers efficiently, how to deploy applications easily with Capistrano, how to manage your videos with MythVideo, how to mix it up a bit (your audio that is), and even play a few games.

Read this issue