What Is PHP? What Does It Do?
PHP is a server-side technology that builds web pages dynamically. Let’s say you have a product catalog. Instead of updating your web pages each time a product is added or removed, you can store the details in a database and use PHP to query the database and build the page automatically. Nor do you need to create a separate page for each product. Just build one page, and PHP fills in the details. Other uses of PHP include creating login systems, uploading files, and sending emails. Just about every online store, news website, blog, or social networking site uses PHP or a similar server-side technology.
<p>The time in London is <?php $now = new DateTime(); $now->setTimezone(new DateTimeZone('Europe/London')); echo $now->format('g.i a'); ?></p> <p>In Los Angeles, it's <?php $now->setTimezone(new DateTimeZone('America/Los_Angeles')); echo $now->format('g.i a'); ?></p>
The PHP code is embedded in a couple of HTML paragraphs between <?php and ?> tags. Even if you don’t understand how it works, you can probably guess that this code has something to do with dates and time zones. In fact, it displays the current time in London and Los Angeles. It doesn’t matter where you are or when you load the page, as long as the server clock is set correctly, you will always get the correct time in those two cities. If you right-click to view the source code in a browser, you see only the HTML output. All the processing is done on the web server.
In this example, the PHP code does all the work itself. But in many cases, PHP acts as an intermediary to a database. The following diagram outlines the basic process.
This is what happens when a browser requests a web page that uses PHP:
- The web server sends the page to the PHP engine—which resides on the server— for processing.
- If the PHP script doesn’t need to communicate with the database, the PHP engine generates the HTML output, and the web server sends it back to the browser.
- If the script needs to interact with the database—getting the results of a search, or inserting or updating data—the PHP engine communicates with the database server. When the results come back, the PHP engine puts everything together, and the web server sends the resulting web page back to the browser.
A lot goes on, but most requests take only a few microseconds, and then the web page is on its way to the browser with no perceptible delay from the user’s point of view.
Why choose PHP?
PHP isn’t the only server-side technology available. In fact, Dreamweaver has varying degrees of support for seven. Most have similar capabilities, and choosing which is the most suitable for your circumstances isn’t always easy. However, PHP has the following advantages:
- PHP runs on Windows, Mac OS X, and Linux. With only a few minor exceptions, code written on one operating system works on any of the others.
- It’s open source and free.
- It’s widely available.
- It’s relatively easy to learn.
- There’s a large community of active users, so help is rarely far away.
- It’s simple enough to incorporate into a small website, yet powerful enough to drive some of the busiest websites, including Facebook, Wikipedia, and Yahoo!
- In a survey of media executives by the Society of Digital Agencies (SoDA), nearly 50 percent said they regarded PHP as an important tool for their company in 2010. The figure for ASP.NET was 32 percent. Fewer than 10 percent said they regarded Ruby on Rails as important for their company.
- In the same survey, more than 50 percent said they would be hiring people with PHP skills in 2010. The only web-related skills in greater demand were Flash and ActionScript.
So are there any disadvantages in choosing PHP?
Comparing server-side technologies is difficult, but the main disadvantage of PHP is, paradoxically, that it’s easy to learn. Many people copy scripts from online tutorials without understanding the code, often leaving gaping security holes in their websites. PHP is as secure as any other server-side technology, and security-related bugs are usually dealt with very quickly. But just like the electricity in your house, it’s safe only insofar as it’s used and maintained correctly. Fortunately, it’s not difficult to write secure code, and there is emphasis on security throughout this book.
Which database should I choose?
More often than not, PHP is used in conjunction with MySQL, the most popular open-source database, which is fast, powerful, and well suited for use in websites. It’s the database that runs WordPress, Drupal, and Joomla! and is also used by high-traffic websites like Flickr, Facebook, and YouTube.
MySQL is currently owned by Oracle Corporation, one of the leading database software companies, but the Community Edition of MySQL is free. The functionality of the Community Edition is identical to the for-purchase Enterprise version. The only difference is that no support is offered with the free version. But that’s rarely a problem because of the active community willing to offer help online.
Like PHP, MySQL works on all the main operating systems, so you can develop on one system and later transfer your database to another. Also, most hosting companies offer PHP in combination with MySQL. For all these reasons, the combination of PHP and MySQL is used in this book.
PHP and MySQL have become so closely connected that many people think you can’t have one without the other. Unlike love and marriage in Frank Sinatra’s 1950s hit, you can have PHP without MySQL. PHP works with all the main database systems, including Microsoft SQL Server, Oracle, and PostgreSQL. You can easily adapt much of the code in later lessons to work with the database of your choice.