Let’s say you’re maintaining or developing a Web application and you’re getting complaints: The site is buggy and slow, it costs too much to maintain, and development takes forever. What do you do? Think about using PHP.
Today, more people run Apache Web server than any other server, and PHP is the single most frequently downloaded module. PHP has proven to be less expensive, faster, more secure, and easier to deploy than any alternative server-side scripting language.
What is PHP?
PHP (PHP Hypertext Preprocessor) is an HTML-embedded scripting language, much like Cold Fusion or Active Server Pages (ASP). First written in 1995, much of PHP’s syntax is borrowed from C, Java, and Perl, allowing Web developers to quickly pick up the language and write dynamically generated pages.
Why use PHP?
Many Web developers found that existing tools and languages were not ideal for the specific task of embedding code in markup. This prompted the creation of PHP. PHP code was developed for embedding within HTML, offering benefits such as quicker response time, improved security, and transparency to the end user. PHP has evolved into a language geared toward a very specific range of tasks. For Web-based applications, PHP is pretty close to the ideal tool.
PHP vs. ASP
If you’ve already deployed Active Server Pages (ASP), you’re probably familiar with its shortcomings as well as its benefits. The biggest drawback of ASP is that it's a proprietary system that is natively used only on Microsoft’s Internet Information Server (IIS). This limits its availability to Win32-based servers. Also, many people would argue that ASP is slower, more cumbersome, and less stable than PHP.
On the other hand, since ASP uses VBScript, it's easy to pick up the language if you already know Visual Basic. ASP support is also enabled by default in the IIS server, making it easy to get up and running.
Today, PHP ships with a number of commercial products and can be run as an Apache Web server core module, making it far faster than other server-side applications. Netcraft.com claims that PHP is in use on over 5.7 million sites around the world. Comparatively, Microsoft's IIS server currently runs on 5.5 million Web sites and ASP on only a portion of those servers.
Unlike other scripting languages for Web page development, PHP is open source and cross-platform. In addition, it offers excellent connectivity to most of today's common databases, including Oracle, Sybase, MySQL, and Microsoft SQL Server, to name a few. PHP also offers integration with various external libraries, which allows the developer to do anything from generating Flash and PDF documents to parsing XML. PHP will run with Apache or Netscape Web servers on most major platforms, such as Linux, Windows NT, 98, and 95, and with IIS on Windows, making your code highly portable.
Worried about migration of existing code? There are tools for converting ASP into PHP, including asp2php to get you up and running.
Giving PHP the edge
So now you know what PHP is, what it does, and how to port to it—but here's the really cool part. You can compile your PHP code for even greater security and performance by deploying the Zend Encoder Unlimited. Other Zend products, some native to PHP, preparse and optimize code on the fly and have extensive caching capabilities. Compared to Microsoft's IIS, the Zend Encoder's license fee is very simple and relatively cheap, and the Encoder offers better performance and higher portability.
So, how do you start using PHP? Check out the online documentation for an overview and some helpful information about setting up your server and then follow these steps to get underway:
- In a test environment, set up a Web server with the PHP module. You can get Apache for either Windows or UNIX and then get PHP from Zend. Both downloads are free. Follow the installation instructions.
- Be sure that you are configured correctly and then write a sample PHP script and place it in your htdocs directory.
- Access the script from your browser.
Just in case you need some help with the script, here’s a sample to get you started. Save the file as example.php. Then, access the file normally using your Web browser, being sure to use the .php extension. Note that <? and ?> are the beginning and end tags for the script.
<TITLE>Sample PHP script</TITLE>
<center>This is normal html.
<font color=red><? echo “This is PHP embedded in html.”; ?></font>
From within the PHP tags, you can execute any number of commands that make PHP a full-blown language, including creating custom functions and variables, manipulating user sessions, and accessing databases.
A handy guide to help you learn commands is PHP Pocket Reference. Also, be sure to read the php.net manual.
Additional PHP resources
Other useful sites for PHP developers with lots of free scripts include:
Next time, we’ll take a closer look at the PHP language syntax itself and walk through code samples. In the meantime, please post your PHP questions, and I’ll try to answer them.