What’s New in PHP 5.4

May 9, 2012

So PHP 5.4 came out a little while back, and although there aren’t any major changes, there are a couple to be aware of. Most of the changes involve either very advanced PHP programming (e.g., closures now support the $this variable) or somewhat advanced Object-Oriented Programming. But there are a some changes that will affect the average PHP programmer, which I’ll outline here.

Short Array Syntax

Historically in PHP, on used the array() function to create a new array, as in:

$empty = array();
$numbers = array(1, 2, 3, 4);
$states = array('AL' => 'Alabama', 'AK' => 'Alaska', 'AZ' => 'Arizona');

PHP 5.4 now has the short array syntax, which just replaces calls to array() with the array operator:

$empty = [];
$numbers = [1, 2, 3, 4];
$states = ['AL' => 'Alabama', 'AK' => 'Alaska', 'AZ' => 'Arizona'];

This is all good and fine, and matches what you might be doing in other languages (such as JavaScript), but remember that you can only do this as of PHP 5.4. This means that unless you know that any server you’ll be using your code on, now or in the future, must be running PHP 5.4 or later.

<?= Always Available

In previous versions of PHP, if the short open tags setting was enabled, you could do this shortcut to print the value of a variable:

<?=$some_var; ?>

Most commonly you’ll see this when using frameworks and templating tools. As of PHP 5.4, this is now always an option, even if short tags are disabled. The only thing to be careful of with this syntax is that it assumes that $some_var has a value.

Built-in Web Server

In my opinion, the coolest addition is the built-in web server in CLI mode. In fact, I’ve already written about it in Chapter 12 of the third edition of my “[intlink id=”3168″ type=”post”]PHP Advanced: Visual QuickPro Guide[/intlink]”, which I’m currently writing. PHP’s command-line interface (CLI) mode allows you to run PHP commands and PHP scripts from the command-line interface. As of PHP 5.4, that command-line tool also has a basic Web server that can be enabled, letting you test PHP scripts in a Web browser without using Apache or IIS or another Web server application. In short, it just became a bit easier to quickly test some PHP code.

The session_status() Function

PHP 5.4 introduces some new functions, constants, classes, and methods, but the one that most piques my interest is session_status(). This function returns a constant indicating the current session status:

  • PHP_SESSION_DISABLED if sessions are disabled.
  • PHP_SESSION_NONE if sessions are enabled, but none exists.
  • PHP_SESSION_ACTIVE if sessions are enabled, and one exists.

I haven’t played with this yet myself, but imagine it could be a useful debugging tool, at the very least.

Removed Features

Finally, you should be aware that PHP 5.4 has removed several features that have been deprecated for some time. Off these, three are important (in that they used to be important):

  • Safe mode is no longer supported
  • Magic Quotes does not exist (and the get_magic_quotes_gpc() function always returns false)
  • The register_globals directive has been removed

With these three changes, you can officially no longer use features that you hopefully stopped using a long, long time ago.

So there you have the key changes in PHP 5.4, aside from some of the more esoteric things. The changes here aren’t nearly on the same scale as those introduced in PHP 5.1 (e.g. PDO extension enabled), 5.2 (e.g., Filter and JSON extensions enabled), and 5.3 (e.g., namespaces and closures, the Fileinfo extension, the INTL extension), but it’s good that a language that’s been around for so long continues to get tweaked, even in minor ways. Makes you wonder what’s left for PHP 5.5, let alone 6, though?