PHP HOW-TO
Al Dev (Alavoor Vasudevan) alavoor[AT]yahoo.com
v27.0, 07 July 2002
This document tells you howto develop PHP programs and also to migrate
all the Windows 95 GUI applications to powerful PHP + HTML + DHTML +
XML + Java applets + Javascript. The information in this document
applies to all the operating sytems where PHP is ported that is -
Linux, Windows 95/98/NT/ME/2000/XP, BeOS, Apple Macintosh (is the son
of FreeBSD Unix??), OS/2, all flavors of Unix like Solaris, HPUX, AIX,
SCO, Unixware, Sinix, BSD, SunOS, etc.. and mainframe operating sys
tems and on all operating systems where "C" compiler is available.
______________________________________________________________________
Table of Contents
1. Introduction
2. PHP runs on Microsoft Windows!!
3. PHP Download
3.1 PHP Installation on Microsoft Windows 95/98/NT/2000
3.2 Apache Webserver Quick-Install (10 seconds) on Microsoft Windows 95/98/NT/2000
3.3 SQL server for Microsoft
3.4 ASP To PHP Converter - Migration From ASP2PHP
3.5 PHP Installation on UNIX'es and others
3.6 PHP Installation on Apple Macintosh
4. Quick Start steps
5. Major Features of PHP
5.1 Advantages of PHP over Java
5.1.1 PHP works with Java
5.1.2 PHP: Growing by the month
6. Brief History of PHP
7. A Simple Example
8. SOAPX4 and PhpXMLP
8.1 XML/XSLT Publishing Engine
9. PHP Libraries
9.1 Classes and PEAR
9.2 Other PHP Utilities
10. PHPGem package
11. PHP Application Servers
11.1 Build PHP based corporate Intranet and Internet
11.2 Popular PHP Applications
11.3 PHP Web Application Servers
11.4 PHP Template Engines
11.5 PHP based Web-Portal systems
12. Object Oriented Features - public, private, protected
13. HTML Editor
14. IDE tools for PHP
14.1 PHP IDE
14.2 PHP IDE for MS Windows only
14.3 PHP IDE for both MS Windows and Linux
14.4 PHP IDE for Linux only
14.5 PHP Utilities
15. Convert Microsoft ASP scripts to PHP - ASP2PHP
16. ctags for PHP
17. PHP documenter (like Javadoc)
18. Debugging PHP
18.1 Debug with FILE and LINE
19. General purpose programming with PHP
19.1 Standalone MS Windows GUI applications using PHP
20. Web Application Standards - Fusebox
21. Performance benchmarking - PHP, ASP, JSP, Coldfusion
22. Limitations of PHP
23. PHP Tutorial
23.1 Primer on PHP Sessions
23.2 Session Management in PHP4
23.3 Session Propagation
23.4 User Management and Privileges
23.5 Step1: Creating the Users Table
24. PHP Training & PHP Help Desk
25. Related URLs
26. Other Formats of this Document
26.1 Acrobat PDF format
26.2 Convert Linuxdoc to Docbook format
26.3 Convert to MS WinHelp format
26.4 Reading various formats
27. Copyright
28. Appendix A PHP examples
28.1 PostgreSQL large object Example
28.2 User authentication Example
28.3 Network admin Example
29. Appendix B Midgard Installation
29.1 Testing Midgard PHP Server
29.2 Security OpenSSL
30. Appendix C - Debug tool phpcodesite
______________________________________________________________________
1. Introduction
(The latest version of this document is at
. You may want to check
there for changes).
Definition: PHP is a simple, object-oriented, interpreted, robust,
secure, very high-performance, architecture neutral, portable, dynamic
scripting language. PHP has "class" keyword similar to Java's "class"
keyword. And PHP is designed such that it is 5 times faster than
Java, since there is no virtual machine. PHP is very fast becoming a
standard, general purpose, object oriented scripting language. PHP is
not only meant for web applications but also can be used for
developing general applications.
PHP is the next big thing after HTML because today nobody writes
static HTML but uses PHP (generate on the fly, embedded dynamic HTML).
PHP 'kind-of' supercedes "static HTML". PHP is a very powerful
scripting language. PHP will overtake PERL/Python and will replace
PERL/Python. PHP is the next-generation PERL/Python scripting. PHP
can do whatever PERL or Python does and actually does much more, much
better and much, much cleaner.
PHP stands for 'Hypertext Pre-Processor' and is a server side HTML
scripting/programming language. PHP is C-like Web scripting language
runs on Apache, Netscape/iPlanet, and Microsoft IIS Web servers. PHP
is a tool that lets you create dynamic web pages. PHP-enabled web
pages are treated just like regular HTML pages and you can create and
edit them the same way you normally create regular HTML pages. PHP
lets you write simple scripts right in your HTML files much like
JavaScript does, except, unlike JavaScript PHP is not browser-
dependant. JavaScript is a client-side html-embedded language while
PHP is a server-side language. PHP is similar in concept to
Netscape's LiveWire Pro product and Microsoft's ASP and Sun
Microsystem's JSP.
PHP is not only used for creating web applications, but also for
creating general standalone applications.
PHP's strength are :
· PHP is the most superior technology. Other technologies PERL,
Python, Tcl, VB script, ASP are older and inferior technologies.
Even Java/JSP is inferior to PHP.
· Open source
· Easily learned syntax
· Broad database connectivity
· Massive library of contributed extensions
· General purpose scripting language much superior to PERL, VB
script, ASP, JSP.
Why PHP ? PHP is the best because of the following reasons:
· PERL is "YUCKY" (distasteful) because it is hard to read and
maintain the programs. Not object oriented. Forget PERL and goto
PHP. PERL programmers like PHP! PHP is actually a "modern PERL",
but with a different name called "PHP".
· Java is object-oriented but is very SLOW. Java programs run very
slow. Also Java is immensely complex and sophosticated technology
(has many layers like JVM, JIT and others). And complexity and
sophostication leads to problems. If there is Linux on server-side
then why do you need Java in servers ?
· Python is "good" but there are no C like brackets/braces which are
useful for code navigation in vi editor. It is like your hand is
broken if you cannot navigate rapidly the source code using
vi/emacs editor's seek braces/brackets commands. PHP is
technologically more advanced than Python.
· PHP is the best because it is object oriented and has the best
features of C/C++/Java/PERL/Python. It can replace PERL, Python,
Java, C, C++, awk, Unix shell scripting, MS Visual Basic and
others!! PHP runs direct and PHP is written in C.
· Each and every computer programmer knows why PHP is the best. Ask
your nearby computer programmer.
· Only PHP will prevail in the 21st and 22nd century and beyond!
· Why do think every PERL, Python and Java programmers are flocking
to PHP ?? There is a very strong reason.
· Every person in the world wants PHP. PHP blast is causing shock-
waves around the world!!
· The world is dominated by PHP. There is PHP everywhere and
anywhere... PHP, PHP, PHP everywhere..
PHP was kept the "top secret and strictly confidential" computer
language by many companies in the world, but now had become the most
well-known and most widely used object oriented scripting language for
web, internet, e-commerce, general purpose and business-to-business
projects. Even today many competing companies keep PHP language as a
highly confidential matter not disclosing to outsiders (competitors).
PHP will storm the entire world and will take the IT industry by
surprise!! The power of PHP is that it is cross-platform and runs
everywhere!! It runs on Linux, Windows 95/98/NT/2000/XP, Solaris,
HPUX and all flavors of UNIX. PHP is write once and deploy anywhere
and everywhere. It runs on many web-servers like Apache, Microsoft
IIS, etc..
PHP runs 5 to 20 times faster than Java!! In actual benchmarks, PHP
was about 3.7 times faster than JSP (see ``''). PHP is extremely easy
to use and you can develop very complex web/e-commerce/general-
standalone applications very rapidly in a very short period of time.
(In future PHP language will imitate most features of Java language
and Java programmers will love PHP. And PHP will have java keywords
like class, extends, interface, implements, public, protected, private
etc..).
It has object oriented features and takes the best features from Java,
C++, PERL and "C" languages. PHP language is a marriage of best
features from Java, C++, PERL, Python and C.
PHP is the real gem of all the scripting/programming languges and will
soon become the "MECCA" for programmers world-wide!! PHP has a huge
user base and a large developer base as it runs on both
Window95/NT/2000/XP and all flavors of UNIX'es.
A big surprise is waiting for us - Most probably PHP will be the
computer language/scripting language of the 21st century!!
PHP can be compiled and optimized to make it run even faster by using
the Zend Optimizer. Zend optimizer is integrated with PHP in PHP
version 4.0. First, you will write your application in PHP scripting
language during development, testing and debugging. Once the project
is ready for deployment you will use the Zend compiler to compile the
PHP to create executable which will run very fast.
You would normally use a combination of PHP (70% code) +
HTML/DHTML/XML (25% code) + Javascript (5% code client side
validations) for your e-commerce projects.
2. PHP runs on Microsoft Windows!!
PHP initially started on UNIX platform, but it is very portable and
runs on MS Windows and MS IIS webserver. Today PHP has a large user
base on MS Windows 2000/NT/95/98, You will find a huge collection of
tools for PHP under MS Windows platform.
Many PHP programmers develop code on MS Windows and deploy on large
linux servers like IBM mainframe running linux, Compaq DEC Alpha and
Sun sparc.
A great advantage is that since PHP also runs on UNIX/Linux,
developers on UNIX platform "cash on" the user base of PHP under MS
windows as the PHP code developed under MS Windows can be used on
UNIX/linux without any code change!!
PHP itself is written in 100% "C" langauge, and hence it runs on a
very wide variety of platforms like BeOS, UNIX, MS Windows, Apple
Macintosh, IBM OS/2 and on many more operating systems.
PHP is very fast and is much faster than Java. For web development,
forget Java/JSP, it is PHP, PHP and PHP everywhere!! PHP is also
becoming a general purpose object oriented scripting language
3. PHP Download
· PHP main site
· PHP resources
· PHP Code Exchange -
· Vex Net
Mirror sites are in many countries like www.COUNTRYCODE.php.net, where
COUNTRYCODE is like us, fe, sk etc.
·
·
·
· Questions e-mail to : rasmus@lerdorf.on.ca
3.1. PHP Installation on Microsoft Windows 95/98/NT/2000
PHP is IMMENSELY POPULAR on Microsoft Windows platform and is
surprisingly more popular than Microsoft's own ASP web scripting
language!! A major reason for popularity is that PHP is a object
oriented scripting language whereas ASP is not. PHP has a large
collection of re-usable classes (objects). PHP runs lot faster than
ASP on MS Windows and has more features and functionalities than
Microsoft ASP. PHP is much more robust, reliable and powerful than
ASP. And the user base of PHP is extremely large because PHP runs on
MS Windows, Linux, Mac OS and all UNIX'es. Greatest advantage of PHP
is that you can develop on MS Windows and deploy on Linux or UNIX and
vice versa!!
There are more PHP users under MS Windows98/NT/2000 than on any other
operating system!! Because there is so much demand for PHP on MS
Windows 98/NT/2000, a ready to install executable is made and you
simply double-click on the exe file to automatically install PHP in
just 2 minutes. Download the PHP executable install file from
· MS Windows exe installer for PHP
· Lots of info on PHP on MS Windows platform
· Install and config of PHP on MS Windows
· PHP Triad installs a complete PHP server environment on Windows
platforms
3.2. Apache Webserver Quick-Install (10 seconds) on Microsoft Windows
95/98/NT/2000
You need a web-server to run the PHP on MS Windows. You can use MS IIS
web server or you can use free Apache web-server for MS Windows
95/98/NT/2000. To save you lot of time here is the ready-to-install
setup.exe file for apache for Windows platform:
PHPTriad which is Apache+PHP+MySQL single package is at
and at mirrorsite
. I very strongly recommend
PHPTriad as it is immensely popular among MS Windows users (millions
of downloads).
Apache binaries -
3.3. SQL server for Microsoft
SQL server can be on a seperate box which need not be running MS
Windows. You also need a SQL server for doing web development. I
recommend that you install Redhat Linux on a very old PC like (Pentium
or 486 box) and install the PostgreSQL
RPMs on it. You do not need any windows graphics for a database server
and at console mode startup the PostgreSQL
server. PostgreSQL
is about 3 times faster than Oracle or MS SQL server.
The PostgreSQL support code for PHP was written by Adam Sussman
asussman@vidya.com
You can also order ready-to-go cheap Linux boxes from -
· Egghead Egghead , click on Auctions and
Linux boxes, you get best deals in live Auctions.
· Goto LinuxHardware and click
ComputerSystems, LinuxOnline , Linux
hardware Crynwr , Linux HarwareNet
· US Land5 , US QLiTech
, US
CompaqLinux , US
VAlinux , US StoreAnywhere
· In Europe : UK GBdirect , UK
MultiT , Hungary Leonardo
, Belgium Mind , Germany
Spier
· DEC alpha linux DECalpha and
CompaqAlphaLinux
You can also get PostgreSQL
for Windows NT/2000 from .
See also the PostgreSQL
howto at pgsql-howto
3.4. ASP To PHP Converter - Migration From ASP2PHP
After installing PHP, you may want to migrate all the Microsoft ASP
programs to PHP. See the chapter ``ASP2PHP''.
3.5. PHP Installation on UNIX'es and others
See the installation guide and instructions at PHP main site
or INSTALL file in the downloaded package itself.
3.6. PHP Installation on Apple Macintosh
Apple Macintosh is Unix like operating system. And actually it's
father is FreeBSD. It picked up many titbits from other Unices and is
quite powerful OS. See the Unix history at Unix Family Tree
and click on the item 12 to see
the Apple Mac position at Apple Mac tree
.
The installation and use of PHP on Apple Mac will be very similar to
that of on Unix OS.
4. Quick Start steps
To implement a project in object oriented PHP do -
· First you need connection to database sql servers - use one of
these:
· ADODB (Active Data Objects Data Base)
· Metabase (Database independent access and management)
· Second you need generic PHP classes to create forms, tables and
other HTML objects. Get these from PHP classes at
. See also the Top downloaded
classes from Top PHP classes
. And see Form
classes, template classes at the site corporate Intranet/Internet
· Third, design and create your own PHP classes by inheriting from
the generic PHP classes .
· Fifth, use IDE tools from ``''
· Most of your PHP code should be in classes for easy code
maintainence and re-usability.
See also Aldev's PHP classes
5. Major Features of PHP
· Standard CGI, FastCGI and Apache module support - As a standard CGI
program, PHP can be installed on any UNIX machine running any UNIX
web server. With support for the new FastCGI standard, PHP can take
advantage of the speed improvements gained through this mechanism.
As an Apache module, PHP becomes an extremely powerful and
lightning fast alternative to CGI programmimg.
· Access Logging - With the access logging capabilities of PHP, users
can maintain their own hit counting and logging. It does not use
the system's central access log files in any way, and it provides
real-time access monitoring. The Log Viewer Script provides a quick
summary of the accesses to a set of pages owned by an individual
user. In addition to that, the package can be configured to
generate a footer on every page which shows access information. See
the bottom of this page for an example of this.
· Access Control - A built-in web-based configuration screen handles
access control configuration. It is possible to create rules for
all or some web pages owned by a certain person which place various
restrictions on who can view these pages and how they will be
viewed. Pages can be password protected, completely restricted,
logging disabled and more based on the client's domain, browser, e-
mail address or even the referring document.
· PostgresSQL Support - Postgres is an advanced free RDBMS. PHP
supports embedding PostgreSQL "SQL queries" directly in .html
files.
· RFC-1867 File Upload Support - File Upload is a new feature in
Netscape 2.0. It lets users upload files to a web server. PHP
provides the actual Mime decoding to make this work and also
provides the additional framework to do something useful with the
uploaded file once it has been received.
· HTTP-based authentication control - PHP can be used to create
customized HTTP-based authentication mechanisms for the Apache web
server.
· Variables, Arrays, Associative Arrays - PHP supports typed
variables, arrays and even Perl-like associative arrays. These can
all be passed from one web page to another using either GET or POST
method forms.
· Conditionals, While Loops - PHP supports a full-featured C-like
scripting language. You can have if/then/elseif/else/endif
conditions as well as while loops and switch/case statements to
guide the logical flow of how the html page should be displayed.
· Extended Regular Expressions - Regular expressions are heavily used
for pattern matching, pattern substitutions and general string
manipulation. PHP supports all common regular expression
operations.
· Raw HTTP Header Control - The ability to have web pages send
customized raw HTTP headers based on some condition is essential
for high-level web site design. A frequent use is to send a
Location: URL header to redirect the calling client to some other
URL. It can also be used to turn off cacheing or manipulate the
last update header of pages.
· On-the-fly GIF image creation - PHP has support for Thomas
Boutell's GD image library which makes it possible to generate GIF
images on the fly.
· ISP "Safe Mode" support - PHP supports an unique "Safe Mode" which
makes it safe to have multiple users run PHP scripts on the same
server.
· Many more new features are being added in newer releases of PHP.
Visit the main web site at
· It's Free! - One final essential feature. The package is completely
free. It is licensed under the GNU/GPL which allows you to use the
software for any purpose, commercial or otherwise.
5.1. Advantages of PHP over Java
PHP is an established server-side scripting language for creating
dynamic Web pages. As a language that has been designed expressly for
the Web, it brings many features that commercial entities are looking
for:
· Exceptionally short learning curve
· Quick development time
· Very high performance
This is essential for companies who are faced with scarce skilled
programming resources and ever-tighter time to market deadlines. In
addition, PHP supports all major platforms (UNIX, Windows and even
mainframes), and features native support for most popular databases.
All these factors make it a very good choice for Web development: such
shops working with PHP have reported being able to hire non-
programmers and have them producing usable code within days.
Programmers familiar with languages such as C, C++ or Java frequently
find that they can begin programming in PHP within a few hours.
The fact that PHP was designed specifically for Web development gives
it an edge as a development tool, as Intranet Design Magazine
explains:
"PHP was built with the needs of Web developers in mind... Unlike
other cumbersome, overhead-laden approaches, PHP is lightweight and
focused on the Web - where it can solve complex problem scenarios
quicker and more easily than comparable technologies."1
Java began its life as a programming language and was initially aimed
at running client-side applications inside of Web browsers. Over the
last few years, the problems associated with these "applets," as they
were known, has led Sun and many other Java developers to use the
language in other ways. One of the most prominent ways is in server-
side programming, allowing Web sites to connect to databases and
produce other server-side applications, or "servlets." Sun came out
with Java Server Pages (JSP) as a means of writing servlets. JSP, like
PHP, is highly portable across different platforms (even though PHP
supports any 32-bit or better platform, whereas JSP supports only
platforms that have a Java virtual machine available). However, a
beginner with no programming background will find it exceptionally
difficult to begin working with servlets,because of the complexity of
the language as well as the complicated JSP system design.
JSP's performance also leaves much to be desired: a recent survey in
ZDnet's eWeek online publication found that PHP is as much as 3.7
times faster than JSP. In the Web environment, this makes JSP a
significantly worse solution - because it can perform less
transactions per second, and features considerably slower response
times.
ZDnet's eWeek also noted that
"JSP is a relatively new technology and still has clear market
immaturities in terms of tools, support resources and available
talent."...Because JSP is based on Java, a strongly typed language, we
had to declare the types of variables we used and manually convert
between types when necessary. This makes JSP the hardest of the
scripting languages we tested for nonprogrammers to learn..."
5.1.1. PHP works with Java
It is important to note JSP is just one language with which Java
objects can be accessed. PHP also can also integrate with Java, which
means that it can be used as the scripting language for activating
Java logic, just as much as JSP can. Unlike JSP, however, PHP also
integrates with other component models, such as Microsoft's COM, and
adding support for new object models is extremely easy. This means
PHP's extensibility is infinite. JSP, on the other hand, is completely
dependent on Java.
IBM programmer Sam Ruby uses PHP in combination with Java. Using the
native Java support in PHP, Java objects can be created and used
transparently, as if they were regular PHP objects.
AuctionWatch , which enables businesses of all sizes to benefit from
dynamic pricing environments, has over 2.6 million unique visitors per
month; 300K+ registered users; 1.2 million+ auctions launched every
month. They tried to use Java, but opted for PHP because it's easier
to use and much faster to develop with. For further information,
please contact Ben Margolin, Director of Seller Services for
AuctionWatch (ben@auctionwatch.com).
Programming consultant Reuven Lerner has made some comparisons among
the languages. His view of PHP is summarized as follows:
"The language is easy to learn. Graphic artists and Web designers
might claim that they are not programmers, but the fact is that
they're doing increasingly large projects in JavaScript, VBScript, and
other simple programming languages. PHP is much more regular and well-
defined than Perl, and is much easier for new programmers to grasp
than Java. Indeed, PHP seems like a server-side version of what
JavaScript could have been, but isn't. Someone with little or no
programming experience can learn PHP and work with it after a very
short period of time. This is probably true for VBScript, but
certainly not for Perl or Java....PHP is a truly remarkable and
powerful system. I can see why it has become a popular system for
producing Web sites.... I think that it will be a very strong
competitor against Java and JSPs, and will rise significantly in my
recommendations for clients and internal use."
5.1.2. PHP: Growing by the month
JSP enjoys the backing of Sun; hence its constant media attention. It
is interesting to note that despite the lack of any publicity
campaign, PHP's popularity is growing at a monthly rate of 8-11%, a
percentage far greater than the Internet's 4% monthly growth -- as the
demand for server-based applications has risen. More than 6.5 million
sites use PHP (Netcraft May 2001 survey). PHP is used on 39% of Apache
servers; Perl trails fourth, with 11% of the market. Apache maintains
59% of the Web server market; Microsoft IIS is second, with 28% of
market share (E-Soft Inc./SecuritySpace's May 2001 report).
Prominent corporations using PHP include Unilever, Philips, NTT,
Cisco, Japan Airlines, Air Canada, Lufthansa, GE Marketplace,
Wallstreet:Online Germany; AuctionWatch, VillageVoice, Dialpad and
BMC.
1. Intranet Design Magazine: PHP From an IT Manager's Perspective, Jun
4, 2000.
2. eWEEK: Four scripting languages speed development, October 30,
2000.
6. Brief History of PHP
PHP began life as a simple little cgi wrapper written in Perl. The
name of this first package was Personal Home Page Tools, which later
became Personal Home Page Construction Kit.
Old name is Professional Home Pages (PHP) and new name is PHP
Hypertext Pre-Processor.
A tool was written to easily embed SQL queries into web pages. It was
basically another CGI wrapper that parsed SQL queries and made it easy
to create forms and tables based on these queries. This tool was named
FI (Form Interpreter).
PHP/FI version 2.0 is a complete rewrite of these two packages
combined into a single program. It evolved to a simple programming
language embedded inside HTML files. PHP eliminates the need for
numerous small Perl cgi programs by allowing you to place simple
scripts directly in your HTML files. This speeds up the overall
performance of your web pages since the overhead of forking Perl
several times has been eliminated. It also makes it easier to manage
large web sites by placing all components of a web page in a single
html file. By including support for various databases, it also makes
it trivial to develop database enabled web pages. Many people find the
embedded nature much easier to deal with than trying to create
separate HTML and CGI files.
Now PHP/FI is renamed as PHP.
7. A Simple Example
Suppose you have a form: