| Understanding
PHP: Part 1 | Part
2
Contributor:
Paul Oldham
Join
Paul Oldham as he introduces the world of Internet
PHP in this extended article. PHP is an open source
server-side scripting language which can be embedded
inside HTML as a clever means of providing dynamic
web pages that C and Perl developers will find particularly
intuitive to learn.
Introduction

PHP, the PHP Hypertext Processor, is an open source
server-side scripting language for Web servers,
which provides a real alternative to ASP, Cold-
Fusion, Mod_Perl or JSP if your aim is to provide
dynamic Web pages. Dynamic Web pages are pages which
interact with the user, so that each user visiting
the page sees customized information - which may
vary each time and which may be based on a form
they’ve just filled in, or on information
extracted from a database or some other external
source. Typical applications include e-commerce,
online newspapers, visitors’ books, ticketing
systems, project management, and other groupware
projects. The traditional way to produce this type
of dynamic page is via CGI scripts, but these are
separate programs which must be executed as a new
process for each page hit, so they scale badly and
rapidly become memory and processor hogs as server
load increases.
PHP solves this problem by becoming a part of the
Web server, essentially extending the functionality
of the server itself, so that the server can do
the processing without having to spawn extra processes.
It’s not alone in doing this, but unlike most
other scripting languages for Web page development
PHP also offers excellent connectivity to most of
the databases in use today. Perhaps the greatest
advantage of PHP, when compared to other scripting
languages such as ASP or ColdFusion, is that it
is open source and cross-platform. PHP’s natural
home is on Linux servers running Apache server software,
but it runs equally well on any other Unix or Windows
platform, and can be used with other Web servers.
PHP started life as a Perl program written by Rasmus
Lerdorf to track visitors to his online resume. It
was then rewritten in C and was extended to include
support for database access. From these simple beginnings
the open source community has expanded and developed
PHP into a powerful server-side scripting language.
Writing PHP

What sets PHP apart from its origins in Perl is the
ability to embed PHP code inside HTML, allowing you
to mix HTML and PHP in one source file rather than
having to crank out the HTML from within Perl. For
example, a simple PHP script to show today’s
date is shown in Figure 1 below. If you’re familiar
with HTML then the top and bottom of this code will
look very familiar - it’s just standard HTML.
| <html>
<head>
<body>
<p>Today is
<?php
echo date("F dS, Y");
?>
</body>
</html>
|
Figure
1
A simple PHP script to show today’s date. |
PHP code is embedded within the HTML by bracketing
it with “<?php” and “?>”.
The code consists of one line, a call to the PHP function
“date”, which returns a date in the format
we’ve specified as a string, which is then output
as part of the HTML using the “echo” function.
This ability to embed PHP inside HTML means that you
can continue to write your Web pages as you do now,
and then embed PHP into them to provide the dynamic
elements.
This is perhaps a good point at which to highlight
what is meant by a server-side language. The PHP code
is executed on the server, not the browser. The source
of the page the browser receives back from the server
is shown in Figure 2.
| <html>
<head>
<body>
<p>Today is
August 30th 2000
</body>
</html> |
| Figure
2 - The source of the page the browser receives
back from the server. |
You can see that the PHP has been processed by the
server, so no special software is needed on the client
to view pages with PHP elements - the intelligence
lies at the server end. This means you can use PHP
with any browser - even simple, text-only ones like
Lynx and w3m, or browsers in PDAs or set-top boxes.
So long as the browser understands HTML it can display
pages written in PHP.
PHP Syntax
In view of where it started, it’s not surprising
that PHP shares syntactical ideas with Perl and C,
and if you’ve programmed in either of these
or you’ve done any Unix shell programming, then
PHP should be easy to pick up. PHP uses loosely typed
variables which don’t need to be declared before
first use, and evaluates them according to context,
so you can add a numeric string to a numeric to give
a numeric, and then manipulate the result as a string.
The PHP online manual gives a nice example of how
a variable’s type can change due to context.
This is shown in Figure 3.
| $foo
= "0"; // $foo is string (ASCII
48)
$foo++; // $foo is now the string "1"
(ASCII 49)
$foo += 1; // $foo is now an integer (2)
$foo = $foo + 1.3; // $foo is now a double
(3.3)
$foo = 5 + "10 Little Piggies";
// $foo is an integer (15)
|
| Figure
3 - A variable’s type can change depending
on context. |
One of the joys of PHP is that some variables are
already set for you in any PHP script. For example,
the variable $HTTP_USER_AGENT will contain a string
identifying the browser which requested the page.
So, for example, if you’re using Netscape 4.7
under NT4 to request the page, $HTTP_USER_AGENT will
contain the string “Mozilla/4.7 [en] (Win NT;
I)”. So if you need to generate HTML which exploits
browser-specific features it’s easy to do. Similarly,
any cookies you have set will automatically appear
as variables in your script without you having to
write any code to extract them. As well as simple
variables PHP also lets you create arrays which act
both like hash tables (associative arrays) and indexed
arrays (vectors), and these arrays can be multi-dimensional.
PHP has a full range of comparative, logical and bitwise
operators, auto increment and decrement, and assignment
operators. It has all the control structures you might
expect from a procedural language, including if/else,
switch/case, for, while (test before) and do (test
after). You can write your own functions or use the
extensive range of functions provided as standard.
Arguments can be parsed by value or reference, and
functions can return a value - or multiple values
if you use an array as the return variable. Variables
are local to a function unless declared global.
PHP also supports include files, both static and dynamic,
allowing common code such as custom function libraries
and HTML fragments to be embedded in your code. This
is all very good news from the developer’s viewpoint,
allowing code re-use wherever possible. It also means
you can separate the PHP code from the HTML if you
employ different people for Web design and Web programming.
PHP3 includes support for object oriented programming,
but there is a problem with polymorphism: once a parent’s
function is overridden by a child it can no longer
be used. This is resolved in PHP4, which provides
expanded functionality and new features for object
oriented programming, and for building classes and
objects.
Part 2 of this extended article on PHP introduces
forms, functions, obtaining PHP, and general PHP4
features.
Copyright Notice
Paul Oldham © 2004 All Rights Reserved.
This
article must not be reproduced without the explicit
permission of the author.
Understanding
PHP: Part 1 | Part
2
|
|
|
|