10 PHP Features You Should Not Live Without

10 PHP Features You Should Not Live Without

We love a lot of different programming and scripting languages out there but PHP has to be one of our favourites. As PHP support has gained popularity over the years so has it’s abilities, being refined and further improved with each release. Today we are going to look at some of our favourite PHP features, some new and some older, that if you haven’t heard of or used are well worth taking a look at.

PHP Data Objects (PDO) - PHP 5.1+

PDO has to be one of the best PHP advancements in recent years. Before it arrived we would have had to write our own classes and functions for each database system we wanted to connect to, or use a third party database abstraction layer of some kind. Of course to be useful PDO has to support the database you wish to connect to, at the time of writing the list of available drivers looks a little like this…

  • Microsoft SQL Server and Sybase (older driver)
  • Firebird
  • IBM
  • Informix
  • MySQL (Percona, and MariaDB)
  • Microsoft SQL Server (supports Microsoft Azure)
  • Oracle
  • ODBC and DB2
  • PostgreSQL
  • SQLite
  • 4D

Quite an impressive list. While it doesn’t support every database system out there, especially with the influx of non-RDBMS systems, it covers the important ones from a web development point of view. It’s accessed using a PHP class which also means it can be extended as required.

Date and Time Classes - PHP 5.2+

The newer DateTime class, and related classes, in PHP are a massive improvement over what went before, and we have found them very useful - and much easier to use - in a number of projects. The regular procedural functions are still available but since this set of classes made it’s debut we haven’t looked back once. The classes available to manipulate the date and time are:

  • DateTime - The main date and time manipulation class.
  • DateTimeImmutable - Same as DateTime but always returns a copy.
  • DateTimeInterface - Useful methods for formatting and working out offsets.
  • DateTimeZone - Very useful for all things timezone related, we use this a lot.
  • DateInterval - Best way to add periods of time onto a date object.
  • DatePeriod - Allows for iteration over a specific period with set steps.

Command Line Use - PHP 4.3+

Whether you run on Linux, Mac OS X, or Windows you will be able to make use of PHP on the command line in some shape or form. This has allowed us to write all sorts of cron scripts and other useful utilities that can make use of PHP features like PDO. It’s never been so easy to write database transactions in a shell environment.

Apart from the obvious functions that require a browser request or web server, it’s possible to use PHP on the command line to do pretty much anything you want. Combined with the process control functions - mentioned next - you can do some very powerful things indeed.

Process Control Functions; PCNTL/POSIX - PHP 4.1+

There are quite a few process control functions in PHP, however the ones that we have made the most use of belong to the PCNTL and POSIX families. Using these functions we have been able to created full blown multi-threaded server daemons. The advantage to using these particular process control functions gives us a very high performance system, especially when designing something that might otherwise be put into a cron task.

With a PHP server daemon - or any other multi-threaded daemon for that matter - you have a command line process that is running constantly in the background. It can spawn child processes to take care of any task based on conditions set out in the code. These are totally up to the developer and could be timed, event base, or anything else you can think of.

There are also some great frameworks out there which build on top of PHP’s process control functions; phpDaemon, System_Daemon (PEAR), and PHP-Daemon by Shane Harter. Depending on what you want it’s worth hunting around or if you only want something light then roll your own.

SimpleXML - PHP 5.0.1+

When you don’t need a fully fledged document object model as provided by PHP’s DOM functions SimpleXML is the perfect answer. Using this extension allows you to quickly read in most XML documents and have it converted to a PHP object which you can then access the properties of in the normal way. SimpleXMLIterator provides a nice way to - you guessed it - iterate over a SimpleXMLElement object, very handy when you need to get at the data quickly with a minimum of fuss.

It’s not just for reading though, it’s also very easy to create an XML document from scratch. In some instances we have used SimpleXML to create an XML document to post to a remote SOAP server as for whatever reason our SOAP calls from PHP were not being accepted.

SOAP - PHP 5.0.1+

When you need to consume SOAP services out in the wild or perhaps run your own API the PHP SOAP functions are well worth looking at. There are two main classes to make use of; SoapServer which as you would expect is used to fire up a server process that will allow remote clients to connect to, and; SoapClient which is the other side of the equation. The client connects to a remote service to request data and pull it back.

We will look at creating a SoapServer - along with client - using PHP in a later article as it can be an extremely useful thing to have for all sorts of reasons.

Filters - PHP 5.2+

Filters are a relatively new set of functions in PHP, and while we could all get along just fine without them, they do provide a useful set of additions to the language. Filters come in one of two flavours; validation of data, or to sanitise it. This is especially useful if you are not a regular expression genius and need to validate some data quickly and easily.

Validation does exactly that, it checks to make sure whatever data is passed into the function is valid according to PHP’s internal rules. Using the validation you can check for valid URLs, email addresses, numbers of different types, and IP addresses.

The sanitise filters on the other hand can be used to strip out rogue data that is not wanted. As an example if you had an email address submitted to your website that contained invalid characters you can use the “FILTER_SANITIZE_EMAIL” filter option to strip out non-RFC conforming characters.

While filters should not be your only way to validate and sanitise data, they do add an easy step to the process of accepting user input on websites.


The speed of a website - or mobile app - is of great importance, more now than ever with certain search engines penalising for not loading content quick enough. But at the base of site speed is user satisfaction, if your page or app does not load data quick enough it will only result in frustrated users, which means less users in general. Memcached is a great solution to speeding up a web or mobile application.

In essence memcached is a giant hash table in memory (think associative array). It can sit on one machine or span many, so it scales well from the smallest install all the way up to huge server farms.

PHP provides a complete set of methods to access all aspects of a memcached server, and can be used effectively to load data into cache so each call for that data does not have to put load on your database. Several popular CMS systems have memcached support baked in which is great if you are a web host or run your own servers as it can drastically lower load.

Namespaces - PHP 5.3+

Namespaces in any language are a very handy thing to have. Put simply PHP namespaces allow you to name methods in your classes whatever you like. In the past if you called one of your methods “write” there was a good chance that name might conflict with the name of another function or method elsewhere. With namespaces however you can wrap your class in it’s own namespace which allows you to keep all of your shorter method names without worrying about conflicts.

This makes for smaller code with more meaningful method names where you don’t have to spend 20 minutes thinking of a new name for a class method.

cURL - PHP 4.0.2+

While cURL is not a PHP extension itself the PHP interface to it is indispensable. For those that haven’t used it before this is the description directly from the cURL website:

“curl is a command line tool for transferring data with URL syntax, supporting DICT, FILE, FTP, FTPS, Gopher, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMTP, SMTPS, Telnet and TFTP. curl supports SSL certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, cookies, user+password authentication (Basic, Digest, NTLM, Negotiate, kerberos...), file transfer resume, proxy tunneling and a busload of other useful tricks”

The PHP cURL extension essentially gives direct access to cURL and any data that it is told to retrieve. It can be used in multiple scenarios from downloading website data, accessing APIs, checking for connectivity, the list is endless as the protocol support is so broad. If you cannot find a use for cURL then you aren’t using it properly.