What Is PHP and What Is It for?

Chapter 1 - What is PHP?
byGareth Downes-Powellet al.
Wrox Press 2003

Put simply, PHP is a tool for creating dynamic web pages. Its presence is completely transparent to the end user. PHP is easy to learn, and most importantly, it's easy to implement.

PHP in the Real World

So, PHP creates dynamic web pages, but what exactly do we mean by dynamic?

Today's web sites come in all shapes and sizes. Some provide rich user experiences using Flash animations; others provide interactive page elements created with JavaScript, or a combination of both (or other) technologies. These types of web site can be described as being dynamic as some part of them changes as a result of either a manual or automatic stimulus. PHP is no different in this respect, as it too, reacts to a series of stimuli in a pre determined way. However, there is one primary difference between these two methods.

Dynamic pages created with JavaScript, or other similar technologies, rely on the ability of the web site visitor's own computer to manifest their effects. This is called client-side scripting, the web pages are sent via the web server to the visitor's web browser, and once loaded the scripts begin to work. Because the code that generates these interactions is sent directly to the browser it is open to public scrutiny, and is therefore not the best choice in all situations. A user authentication system, for example, becomes ineffective because the code that handles the login process is completely visible, and therefore insecure.

PHP, however, works in an entirely different way. A web page containing PHP code is "pre-processed" by the PHP engine, called an interpreter, and the results of this processing are passed back to the web server and on to the visitor's web browser. As only the results of the PHP processing are sent to the browser, the code that generated them remains hidden, and is therefore much more secure. This kind of pre-processing is called server-side scripting, and while it doesn't provide the same kind of dynamic effects as JavaScript, PHP pages can be called dynamic. Given the example above, of a user authentication system: one page of PHP code, can process a username and password, determine if these values are valid or invalid and based on this determination, send the user to a login failure page, or to a successful login page.

Dreamweaver MX allows us to harness the power of PHP in a visual way, without delving into the code itself, unless we want to. Because of this, Dreamweaver MX is perfectly suited to the rapid development of dynamic web sites. To better illustrate this throughout the book we will base all our examples around a single case study, which will allow you to work through each chapter and watch the whole process of dynamic web development evolve.

The case study chosen for this book is that of a hotel booking system. This is a good example to use as it allows you to view the process from the perspectives of both the hotel staff and the customer. Both parties will interact with the web site in totally different ways. The customer will see the web site only via its public interface, where they will be able to find out more about the hotel itself, and make room reservations. The hotel staff, however, will have additional access to a private administration area where they will be able to view the customer bookings and more.

The Hotel Booking System

Booking a hotel room is now a very common practice on the Internet. It is quick and easy. To book a hotel room, the customer visits the web site, enters the date of arrival, date of departure, and the type of room required. Using this information, the web site searches the reservations database to ensure that there is an appropriate room available for the dates specified. When the search is complete the customer's reservation is either denied (if no rooms are available), or confirmed in which case the customer then proceeds to enter their credit card details to secure the booking.

As you can see from the above example, this is a type of dynamic web site. The customer provides the stimulus (the dates of the stay, and the type of room required), and the web site dynamically determines whether those requirements can be met. This type of web site is more commonly known as a database-enabled web site, as all of the information that the web site needs is stored on a database. Through the magic of PHP this information is retrieved, searched, sorted, and evaluated to determine the appropriate response.

Throughout this book, we will return to this example, and explore elements of it in more detail. We will look at the development tasks required to develop just such a system. All of the principles used in the book are applicable, regardless of the type of project you are working on.

A Brief History of PHP

From its earliest beginnings PHP was designed for environment-independent development. Able to run on many different web servers and operating systems, it is an easy-to-learn and very flexible development tool.

Born from a series of Perl (or CGI - Common Gateway Interface) scripts written by Rasmus Lerdorf in 1995, PHP was initially two independent tools. Personal Home Page Tools was written purely to track the number of visits to the author's online resume. To increase performance and provide greater stability, these scripts were soon rewritten in C. The functionality they provided soon attracted the attention of other developers using the same web server. Rasmus allowed these developers access to his scripts, which proved to be so popular that he was soon receiving requests for additional features. Rasmus was also interested in the use of databases wherever possible, and had developed another series of Perl scripts called Form Interpreter (FI). These scripts processed requests to databases and routed the information back to a web page, to create interactive, dynamic forms.

Soon, both PHP and FI were merged into a single unified tool. This tool was called PHP/FI v2, and was the forerunner of PHP as we know it today.

In 1997, as the development of PHP/FI continued to escalate, it became apparent that the project was becoming larger than one man could handle and the development of PHP was opened up to a group of developers. Two members of this group, Zeev Suraski and Andi Gutmans were responsible for a complete rewrite of the core PHP code, which became known as the Zend engine, a name derived from both author's names. This engine set out the architecture and functionality for all future versions of PHP.

Along the way the meaning of PHP itself also changed; it became a recursive acronym, one where the acronym's meaning refers to itself. PHP now stood for "PHP: Hypertext Preprocessor".

The power and flexibility offered by PHP 3 grabbed the attention of the web professional, and PHP became a more mainstream development option. Due to this huge increase in popularity, the Zend Engine was rewritten once again, with further enhancements and functionality. This final rewrite forms the basis of the current PHP versions, version 4.

Before looking more closely at PHP, it really is only fair if we take a step back and look at the other options available. PHP is by no means unique in what it does, in fact there is a whole wealth of different technologies available, and like PHP, all have their strengths and weaknesses. Some of these technologies are open source, or free for developers to use in non-production environments, whilst others require you to purchase a license before you can begin to learn about them. In fact, there are so many different options that delving deeper into this subject will reveal quite a number of possibilities!

Rather than discuss all of these available options, we shall, for the purpose of this book, concentrate solely on the other scripting languages that are supported by Dreamweaver MX.

The Alternatives

With everything in life, no matter what you decide to do, there is always another way to achieve the same goal. Creating a database-enabled web site is no exception. All of the different technologies available achieve, ultimately the same thing, but they all do it slightly differently.

The main alternatives are Active Server Pages (ASP), Java Server Pages (JSP) and Cold Fusion Markup Language (CFML). There are, of course, others, but these four are the main contenders in terms of popularity (of course, all of these alternatives are also natively supported within Dreamweaver MX). All of these technologies have their own strengths and weaknesses, as does PHP.

Whilst the majority of alternatives are commercial in nature, some are more developer-friendly than others, providing free development-only versions, which are useful for testing and learning. However, in a production environment, there are usually costs associated with the installation of the relevant production-level server.

ASP, for example, requires that you purchase a version of Windows that comes with Internet Information Server (IIS). Windows NT Server, Windows 2000, and Windows XP Professional all come with a version of IIS, but it can prove to be an expensive option when installing a production server of your own. There are alternatives, however, so that ASP pages can be used on other web servers, such as Apache. Amongst these are ChilliSoft ASP, and iASP, both of which are fairly popular. Though still commercial in nature they do provide a cheaper alternative and do not commit you to the use of a Windows-based web server.

ASP has proven to be a popular choice amongst developers, as there are many relatively cheap hosting companies that provide ASP hosting via a shared server, where multiple users and web sites exist on the same machine. In such an instance, you are not required to buy any form of Windows, and ASP really does become a viable option.

ASP.NET is similar in nature to ASP, in that you must have an appropriate Windows-based operating system and web server to process ASP.NET pages. However, Halcyon Software, the developers of iASP, are currently working on a version of the .NET framework called iNET, for use on alternative web servers. This will ultimately lead to another commercial program, but does open up more possibilities for development. For more information on iNET see http://www.halcyonsoft.com/products/iNET.asp. As the popularity of ASP.NET grows, it will undoubtedly become just as viable an option as ASP. As more hosting companies provide ASP.NET support via a shared server, the price of ASP.NET hosting will reduce dramatically.

JSP is slightly different in that there are both commercial versions of JSP as well as open source versions. This is good as you can experiment with versions of JSP, to discover which one suits you best, and as you aren't tied to one particular operating system, as there are versions of JSP available for most forms of web server. Unfortunately, JSP pages haven't proved to be a popular option, not many hosting companies provide JSP support, and those that do usually charge a premium for the service.

CFML is a totally commercial server option. If you wish to install ColdFusion onto your production server, then you have to purchase the ColdFusion software, which is available in Professional and Enterprise versions depending on your requirements. Whilst there are flavors of ColdFusion that will work on a number of different web servers, it hasn't proven to be a popular option amongst host providers either, as even in a shared server environment it is a costly option. If you wish to use ColdFusion locally, for development or learning purposes, there is a free development version available, but this version is feature-limited to prevent more than one user visiting the generated web pages.

PHP, in contrast, is completely free to use either privately or commercially, and for this reason there are no associated costs in running PHP on your own web server. PHP, like some of the other server technologies, can also be installed on a number of different web servers, and due to its easy installation, and lack of price tag, it has proven a very popular choice amongst host providers.

ASP

Active Server Pages is a proprietary technology, based around Microsoft's Internet Information Server. It allows you to combine HTML, Visual Basic Script (VBScript), and JavaScript with reusable ActiveX Components to create dynamic web pages.

For many developers this presents the ultimate in dynamic web page development because of the tight integration and interoperability between all of the components. Historically, ASP has proven to be costly to implement, due in part to its reliance on other Microsoft products. However, this is no longer a real issue, thanks to the large number of companies offering cheap ASP hosting packages.

In comparison to PHP, ASP can be unnecessarily unwieldy and slow. Much of the functionality that PHP offers as standard is not integrated into ASP's core code, which brings about a reliance on third-party code to extend and enhance it. ASP can present a very steep learning curve, especially to the first-time developer.

ASP.NET

Like ASP, ASP.NET is another proprietary technology, built around Microsoft's Internet Information Server and its own server variations called the .NET Web Server. Whilst ASP.NET is a relatively new kid on the block, it is a more developer-friendly language and is specifically geared towards the rapid development of web applications.

To achieve this, it provides the flexibility of allowing you to decide which programming language you wish to work with, with support for 25 different programming languages. It provides a wealth of new functionality, previously unavailable to ASP developers, and offers enhanced performance and reliability.

ASP.NET really is more of a programming language than a scripting language, and for this reason it is attracting more and more application developers every day.

Unfortunately, as ASP.NET is still relatively new, it is costly to implement in a production environment. There are only a very limited number of companies providing .NET support, though this number is slowly increasing, and while other companies are implementing versions of the .NET Framework for other servers, such as iNET, it will still be some time before this is a truly mature environment for development.

ASP.NET can prove to be especially difficult for the first-time developer to understand, and part of this is due to the number of languages that can be used for ASP.NET development.

Conversely, PHP is very easy to learn, especially if you are already familiar with technologies such as JavaScript. It is a stable and mature development language, which, like ASP.NET, is well suited to the rapid development of web applications. Whilst PHP does lack some of the complexity of ASP.NET, and what I am sure will prove to be flexibility in the future, PHP is constantly evolving and embraces new functionality wherever it is available. So much so that newer versions of PHP are even able to take advantage of elements of the .NET framework with relative ease.

CFML

ColdFusion Markup Language might sound like something that you write on the side of a nuclear reactor, but the truth is, that this is an extremely powerful and easy-to-learn language, as it is similar in many respects to HTML. Originally developed by Allaire in 1995, CFML started, like PHP, as a set of Perl scripts. It evolved rapidly into a full application server, and in 2001 the ever-evolving Macromedia purchased Allaire. At the time of writing the most recent version of the ColdFusion server is ColdFusion MX.

CFML grew in popularity for its clarity and ease of use. It is a tag-based language, like HTML, which makes it extremely easy to learn and fast to implement. CFML also has another great strength: it is extremely portable. When creating database connections, for example, often the code needs no modification when the pages are moved from a development server to a production server.

One other strength of CFML is the ability to create custom tags. This might sound a little daunting, but essentially, this feature allows any user at any time to take a portion of their own code, and create a tag that automatically executes their custom code. It is not unusual for a Cold Fusion Developer to have hundreds of "tag libraries" as they are known, as they increase the speed of development massively.

Unfortunately, some developers cite the very strengths of ColdFusion, highlighted above, as its ultimate weakness. Many developers don't like the idea of a tag-based language. It can make it difficult to determine which is server side code (code that runs exclusively on the server, whose output is sent to the web browser), and which is HTML mark up. This lack of structure can make it difficult for the developer with a programming background to really get to grips with ColdFusion. However, the removal of a formal syntax and structure system when using CFML makes it perfect for the neophyte developer.

JSP

Java Server Pages is available in various flavors. For many years Java was touted as being the ultimate in platform-independent coding. This was the promise, and to a degree, that promise was delivered.

The move from Java-based applications to its use as a method of dynamically creating web pages was a logical one. Certainly, the great thing about this architecture is that it can be run on any platform, on any machine, so it is a good candidate for portability and compatibility.

As there are a number of variations, at this point we should clarify how JSP is structured. First of all JSP is what is called a 'reference implementation' of Sun Microsystems Java Server Pages. In essence, Sun Microsystems determine how JSP should be implemented, and have developed a standard by which all versions of JSP should comply, called the Java 2 Enterprise Edition (J2EE).

There are various open-source and commercial implementations of JSP, including Macromedia JRun Server and Caucho Technology's Resin. Because there are various implementations, it can be difficult for the first time user to decide which version they should use. Different versions written by different software houses also bring a huge difference in quality, stability, and speed for each of these environments, as each has its own unique nuances.

In this respect, PHP really does have the edge. As there is a single development base, with a group of individual developers supporting, maintaining, and improving PHP at each stage it is stable, reliable, and much easier to implement than its JSP counterparts. It also means that there is no confusion about which "type" to use, as there is only one PHP.

JSP is a fairly flexible server technology too, as it allows you to extend and enhance its functionality in a similar way to CFML, providing support for third-party extensions, or "tag libraries". Its syntax and usage is also not dissimilar to PHP and JavaScript, as they all have a common inspiration root. However, whilst both PHP and JSP share a common background of sorts, JSP is much more difficult to learn and less user-friendly than PHP.

Dreamweaver UltraDev and PHP

Dreamweaver MX is the first version of Dreamweaver with native PHP support, but PHP developers have been using Dreamweaver UltraDev for quite some time now. How did they manage without the labor-saving tools that Dreamweaver is famous for providing? Well, it took some time, but they invented their own.

Since the very first version of Dreamweaver there has been only token support for PHP. When Macromedia purchased Elemental Software's Drumbeat, redeveloped it into the Dreamweaver Architecture, and released the first version of Dreamweaver UltraDev, there was wide criticism that there was no native support for PHP-generated pages. Developers couldn't understand the oversight, as PHP is currently the second most popular scripting language (source: http://php.weblogs.com/popularity). There was support for three server technologies, dubbed "server models", in UltraDev: ASP, JSP, and CFML. However, while UltraDev recognized PHP code when it was in use in a page, there was no built-in functionality to support this Server Model.

Unfortunately, the situation didn't improve at all for the PHP developer with the second version of UltraDev (confusingly called Dreamweaver UltraDev 4) until the middle of 2001, when a young talented developer called Dan Radigan began work on a project called PHP4UD.

One of the key features of all versions of Dreamweaver is that it is extensible. Using a combination of HTML forms and JavaScript routines it is possible to develop a type of macro called a behavior. Behaviors allow you to add additional functionality to your page in a visual way, without having to worry about the underlying code. Before UltraDev, these behaviors most commonly inserted JavaScript into your page and allowed you to easily add dynamic page interactions. When UltraDev was released, a new type of behavior became available: server behaviors.

Server behaviors work in much the same way as the original Dreamweaver JavaScript behaviors, however, instead of inserting JavaScript code, these behaviors inserted ASP, JSP, or CFML code into your page. Again, this was done in a visual way, and this formula proved to be very successful in developing web applications rapidly.

Along with JavaScript behaviors and server behaviors, there are a number of different types of extensions available as Macromedia wanted to provide as much flexibility as possible to the user. It was even possible to write an entirely new server model from scratch, to provide additional support for another server technology. PHP4UD was the first time that anyone had created this kind of extension.

After a number of beta releases, Dan handed development of this project over to a young Romanian company called Interakt. The next release of this server model saw some significant changes and enhancements to the functionality. To mark the significant leap in functionality the project was renamed to PHAkT.

PHAkT became the only way for PHP developers to fully utilize the power of UltraDev. This was something that was quickly recognized by many developers, who started to develop and share additional extensions for use specifically with PHAkT. These extensions ranged in complexity and design from simple scripts to format data in a specific way, to shopping cart management systems.

click to expand

Recognizing the potential of the PHP Server Model at an early stage, Interakt also began work on an enhanced version of PHAkT, called ImpAKT. As PHAkT was released under the GNU Public License, or GPL, it was freely available to all for commercial or private use without charge. As ImpAKT was to be an enhanced version of PHAkT, it would be released under a commercial license, and provide a way to recuperate the costs associated with the thousands of hours of development time it took to bring PHAkT to its version 1 release.

The changes for PHP developers were massive. Especially for those who were used to not having any option but to hand code their pages. Now, there was not one PHP Server Model to use, there were two.

Thanks to the popularity of the new PHP Server Models, and the support given by the Extension Developer Community, Macromedia have finally provided support for PHP within Dreamweaver MX. This native support for PHP within Dreamweaver comes via Macromedia's own PHP server model, called PHP MySQL.



Dreamweaver MX PHP Web Development
Dreamweaver Mx: Advanced Php Web Development
ISBN: 1904151191
EAN: 2147483647
Year: 2001
Pages: 88

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net