CHAPTER 1
What Is PHPAnd Why Should I Care?
Officially, PHP stands for PHP: Hypertext Preprocessor. Its an ugly name that gives the impression that its strictly for nerds or propellerheads. Nothing could be further from the truth. A lighthearted debate on the PHP general mailing list ( http://news.php.net/php.general ) several years ago suggested changing what PHP stands for to Positively Happy People or Pretty Happy Programmers. This book aims to help you put PHP to practical useand in the process help you understand what makes PHP programmers so happy.
PHP is a scripting language that brings websites to life in the following ways:
- Sends feedback from your website directly to your mailbox
- Uploads files through a webpage
- Generates thumbnails from larger images
- Reads and writes to files
- Displays and updates information dynamically
- Uses a database to display and store information
- Makes websites searchable
- And much more...
By reading this book, youll be able to do all that. PHP is easy to learn; its platform-neutral, so the same code runs on Windows, Mac OS X, and Linux, and all the software you need to develop with PHP is open source and therefore free.
In this chapter, youll learn about the following:
- How PHP has grown into the most widely used technology for dynamic websites
- How PHP makes webpages dynamic
- How difficultor easyPHP is to learn
- Whether PHP is safe
- What software you need in order to write PHP
How PHP Has Grown
PHP is now the most widely used technology for creating dynamic websites, but it started out in 1995 with rather modest ambitionsand a different name. It was originally called Personal Home Page Tools (PHP Tools). One of its main goals was to create a guestbook by gathering information from an online form and displaying it on a webpage. Within three years, it was decided to drop Personal Home Page from the name, because it sounded like something for hobbyists and didnt do justice to the range of sophisticated features that had since been added.
PHP has continued to develop over the years, adding new features all the time. According to W3Techs ( http://w3techs.com/technologies/details/pl-php/all/all ), PHP is used to create dynamic content by more than 80 percent of the 10 million websites it regularly surveys. Its the language that drives highly popular content management systems (CMSs) such as Drupal ( http://drupal.org/ ), Joomla! ( www.joomla.org ), and WordPress ( http://wordpress.org/ ). It also runs some of the most heavily used websites, including Facebook ( www.facebook.com ) and Wikipedia ( www.wikipedia.org ).
One of the languages great attractions, though, is that it remains true to its roots. PHPs original creator, Rasmus Lerdorf, once described it as a very programmer-friendly scripting language suitable for people with little or no programming experience as well as the seasoned web developer who needs to get things done quickly. You can start writing useful scripts without needing to learn lots of theory, yet be confident in knowing that youre using a technology with the capability to develop industrial-strength applications.
Note At the time of this writing, the current version is PHP 5.6. The code assumes youre using a minimum of PHP 5.4, which removed several outdated features, such as magic quotes. If you have a hosting plan, make sure the server is running at least PHP 5.4.
The next major version of PHP will be called PHP 7. Its been decided to skip PHP 6 to avoid confusion with a version that was abandoned in 2010 for being too ambitious. The emphasis in this book is on code that works now, not on what might work at some unspecified time in the future. However, I fully expect that most if not all of the code and techniques will continue to work in PHP 7.
How PHP Makes Pages Dynamic
PHP was originally designed to be embedded in the HTML of a webpage, and thats the way its often still used. For example, if you want to display the current year in a copyright notice, you could put this in your footer:
PHP Solutions
On a PHPenabled web server, the code between the
CHAPTER 2
Getting Ready to Work with PHP
Now that youve decided to use PHP to enrich your webpages, you need to make sure that you have everything you need to get on with the rest of this book. Although you can test everything on your remote server, its usually more convenient to test PHP pages on your local computer. Everything you need to install is free. In this chapter, Ill explain the various options for Windows and Mac OS X. The necessary components are normally installed by default on Linux.
What this chapter covers:
- Checking if your website supports PHP
- Why PHP 5.4 should be the minimum version
- Deciding whether to create a local testing setup
- Using a ready-made package in Windows and Mac OS X
- Where to store your PHP files
- Checking the PHP configuration on your local and remote servers
Checking Whether Your Website Supports PHP
The easiest way to find out whether your website supports PHP is to ask your hosting company. The other way to find out is to upload a PHP page to your website and see if it works. Even if you know that your site supports PHP, do the following test to confirm which version is running:
- Open a text editor, such as Notepad or TextEdit, and type the following code into a blank page:
- Save the file as phpversion.php . Its important to make sure that your operating system doesnt add a .txt filename extension after the .php . Mac users should also make sure that TextEdit doesnt save the file in Rich Text Format (RTF). If youre at all unsure, use phpversion.php from the ch02 folder in the files accompanying this book.
- Upload phpversion.php to your website in the same way you would an HTML page and then type the URL into a browser. Assuming you upload the file to the top level of your site, the URL will be something like http://www.example.com/phpversion.php .
If you see a three-part number like 5.6.1 displayed onscreen, youre in business: PHP is enabled. The number tells you which version of PHP is running on your server. You need a minimum of 5.4.0 to use all the code in this book.
- If you get a message that says something like Parse error Hypertext Preprocessor (PHP)website supportsParse errorit means PHP is supported but that you have made a mistake in typing the code in the file. Use the version in the ch02 folder instead.
- If you just see the original code, it means PHP is not supported.
Official support for PHP 5.3 ended in August 2014. If your server is running PHP 5.3 or earlier, contact your host and tell them you want the most recent stable version of PHP. If your host refuses, its time to change your hosting company.
WHY PHP 5.4 SHOULD BE THE MINIMUM VERSION
As a general principle, PHP tries to preserve backward compatibility between point releases (where only the numbers after the first dot in the version number change). However, a number of outdated features were removed from PHP 5.4. New syntax was also introduced for arrays.
Although most of the code in this book will run correctly on older versions of PHP, you may get unexpected results if you use a server that still relies on those features. The most important changes that affect the code in this book are the removal of safe mode and magic quotes.
Safe mode is often used in shared hosting environments. Among its effects, safe mode restricts where include files can be located and which files can be read from and written to. With the removal of safe mode in PHP 5.4, these restrictions no longer apply.
Next page