by July 6, 2001 0 comments

My romance with PHP began with the
realization that designing plain HTML sites won’t get us anywhere, and there
isn’t much of a demand for Flash. The money lay in designing RDBMS-backed Web
applications. Thus began my search for a language or platform which was:

l Easy to learn

l Didn’t require any proprietary IDE
or complex programs to develop

l Worked on both Linux as well as NT
servers

l Was not browser-dependent

l Was easy to integrate into existing
Websites.

Given my limited skills, PHP won on all
counts–way ahead over Perl, Java, and ASP.

So what is PHP?

True to the genre of GNU
acronyms, PHP stands for PHP Hypertext Preprocessor. Originally, PHP stood for
Personalized Home Pages. To say it in one line–PHP is a server-side, HTML-embedded
scripting language.

Brief history

Rasmus Lerdorf
conceived PHP sometime in the fall of 1994. Early non-released versions were used on his
home page to keep track of who was looking at his online resume. The first version was
available sometime in early 1995 and was known as the Personal Home Page tools. It came
with a simple parser engine that understood only a few special macros and a number of
utilities that were in common use on home pages back then–a guest book, a counter,
and so on. The parser was rewritten in mid-1995 and named PHP/FI version 2. The FI came
from another package, written by Rasmus, which inter
preted HTML-form data. He
combined the Personal Home Page tools scripts with the Form interpreter and added mSQL
support, and thus was born PHP/FI. PHP/FI grew at an amazing pace and people started
contributing code to it. Today PHP 4 is in beta stage.

Using PHP

Once you’ve installed
PHP on your Linux box running Apache, you’re ready to go.

Create a blank HTML file (use anything you
want), rename it to “hello.php3” (could be anything.php3), open it in a text
editor of your choice and insert the following snippet between the < /body> tags.

echo “hello
World!”,

?> We’ve just written the ubiquitous
“helloworld” program. It’s evident that whatever is between
& ?> are parsed on the server. To view your page, place hello.php3 in the
/home/httpd/html directory, fire up your browser and point it to http://localhost/hello.php3.

How about something more complex? Create an
HTML form with a text field “name”. In the action property, add display
name.php3. Now create a PHP file, display name.php3, and add the following code.

echo “you wrote
“,

echo $ name;

?>

On this Web page, after you fill in your
name and submit it, you’ll be shown the message–you wrote (whatever you wrote).
You may have noticed that the form elements are available to the action script as
variables of the same name–in this case $name and whatever you entered in the field
is the value assigned to the variable.

It may also be evident by now that you
don’t need any special tools to write PHP scripts. I personally prefer JOE, but if
you use VIM you can have syntax highlighting as well.

Allaire Home Site under Windows also
supports PHP highlighting. Two more advanced PHP editors–PHPed (www.
soysal.com/PHPEd) and PHP-FUSION (www.sqlfusion.org)–are also being developed.

More PHP features

Apart from ease of use, the
most compelling feature of PHP is integration with databases, from MySQL, Postgre SQL,
Oracle, Sybase, and even DBF. Firing an SQL query is simply a matter of writing something
like:

$result = mysql_query(“SELECT *FROM
tablename WHERE field=$value”);

Here we used a call to MySQL database and
$result will contain the result of the SQL query as an array. Almost all features of RDBMS
are available as relevant functions under PHP, but MySQL appears to be the current
favorite.

If you’re using PHP as an Apache
module, you can use it for HTTP authentication. PHP also supports HTTP cookies.

There is a very convenient “file
upload” feature through which you can upload files to your server and process it
using PHP, depending on the attributes of the file, like file size, type, etc.

The coding for the form for “file
upload” will be something like this:

Send this
file:

The _URL_ should point to a PHP HTML file.
The MAX_FILE_SIZE hidden field must precede the file input field and its value is the
maximum file size accepted. The value should be in bytes. In this destination file, the
following variables will be defined upon a successful upload:

l $userfile: The temporary filename
in which the uploaded file was stored on the server machine

l $userfile_name: The original name
of the file on the sender’s system

l $userfile_size: The size of the
uploaded file in bytes

l $userfile_type: The mime type of
the file if the browser provided this information. An example would be
“image/gif”.

Note that the “$userfile” part of
the above variables is whatever the name of the INPUT field of TYPE=file is in the upload
form. In the above example of upload form, we chose to call it “userfile”.

Files will be stored in the server’s
default temporary directory by default. This can be changed by setting the environment
variable–TMPDIR–in the environment in which PHP runs.

The PHP script that receives the uploaded
file should implement whatever logic is necessary for determining what should be done with
the uploaded file. You can, for example, use the $file_size variable to throw away any
files that are either too small or too big. You could use the $file_type variable to throw
away any files that didn’t match a certain type criteria. Whatever the logic, you
should either delete the file from the temporary directory or move it elsewhere. The file
will be deleted from the temporary directory at the end of the request, if it has not been
moved away or renamed.

PHP has a whole gamut of functions, which
let you use IMAP for all your mail needs. Most of the IMAP functions can be used for POP
as well. Alternatively you can download the free POP class available at
www.phpclasses.Upper design.com/browse.html?package=2. For sending mail, the in-built
mail( ) function is adequate. If you’re looking for a free Web mail program, using
IMAP and PHP, check out IMP at www. horde.org.

PHP till very recently was able to give
data-driven images like graphs and menu buttons, but with the patenting of LZW compression
by Unisys, the GD library, on which PHP relied for GIF creation, outputs only PNG format. Add-ons

Since PHP supports the
Object Oriented Programming paradigm, it allows for convenient code re-usage, a lot of
free (and commercial) classes and libraries are available on the Net, which you can use as
add-ons from within your scripts. The ones I personally find most useful are:

Fast templates (www.thewebmast ers.net/php) (www.thewebmast
ers.net/php)

This class allows you to separate the
visual design of your project from the programming part. This is especially convenient in
large projects where the designers are different from the programmers.

PHPLib (http://phplib.netuse.de)
(http://phplib.netuse.de)

This is a large collection of some very
useful functions. Notable amongst them are functions for session management. Incidentally,
the capability for session management will be built in to PHP 4.

The third class which I’ve been using
of late, after the patenting of LZW compression is HTMLGraphs available at
www.webguys.com/pdavis/Programs/html. _graphs. You can create all sorts of bar charts
using this class. The class utilizes the features of HTML tables to achieve this, rather
than outputting an image.

Resources

There are no books
available in the Indian market till date to learn PHP. The best place to start is the PHP
manual and the tutorials at www.php.net. Two other excellent starting points for PHP are
www.phpbuilder.com and www.webdev. berber.co.il.

What have I done using PHP? Made life
simple. Check out the coupons and classifieds at www.lifemadesimple.com.

Tarique Sani

runs SANIsoft, a Web engineering company based in Nagpur

No Comments so far

Jump into a conversation

No Comments Yet!

You can be the one to start a conversation.

Your data will be safe!Your e-mail address will not be published. Also other data will not be shared with third person.