Cool new stuff in PHP 5.4

PHP 5.4 was released yesterday. It took nearly 2 years since the release of version 5.3 and has been a huge undertaking. Congratulations to the guys that made it happen. Let’s take a look at the features:

1) Speed
PHP 5.4 is the fastest of all PHP versions. Benchmarks both Synthetic and real-life show a pretty impressive jump in performance. Drupal, WordPress, etc are faster by almost 10% while Zend Framework was faster by 21%. There is a lot of improvement on memory usage, performance (req/sec), etc. Andy Gutmans of Zend claimed the memory foot print is also lowered by upto 35 percent (that is huge!). Read more about the tests here

2) Short echo tags
Short echo tags will always work irrespective of the ini settings.

<?= "Always works even if ini short tags are disabled" ?>

3) Compact Array Syntax and Array Dereferencing
Both of these changes makes the code look much more cleaner and easier to read. Take a look at the examples below

$a = [1, 2, 3]; //Same as array(1, 2, 3);

$a = "Zero One Two Three Four";
echo explode(" ", $a)[4]; // outputs Four

//Function Array Dereferencing
function movies() {
return ['Mission Impossible', 'Transformers', 'Titanic'];
echo movies()[1]; //Outputs: Transformers

4) How long did your script take to execute?
Previously you set a start variable and store time in it. Then you write your code and compute the difference in the end. Like this

$time_start = microtime(true);
// Run a lot of your code
$time_end = microtime(true);
echo "Took:", $time_end - $time_start; //Time spent

// Just one line at the end of your script with PHP5.4
echo "Took:", (microtime(true) - $_SERVER['REQUEST_TIME_FLOAT']);

5) Built in Web Server (CLI)
Well, we’ve been strongly recommended not to use this on production as this is brand new and not intended to replace Apache/nginx !

$ php -S localhost:8080

The above shell command gets the web server running at port 8080 after setting the Document Root as the current working directory. It also serves static assets and supports URL routing.

6) Traits
Traits is “compiler assisted copy-paste” literally, it is also known as “Horizontal Reuse” and similar to functionality of “Multiple Inheritance”. This implementation adds 4 new keywords: trait, use, as and insteadof. As and insteadof are used to resolve conflicts when you use multiple traits.

trait sayHello { //First Trait
public function hello() { echo "Hello"; }
trait sayWorld { //Second Trait
public function world() { echo "World"; }

//Then we define the class
class sayHelloWorld {
use sayHello, sayWorld; //use brings in both Hello and World functions

$say = new sayHelloWorld();
$say->hello(); //Hello
$say->world(); //World

No concrete plans for PHP 6 or other versions announced yet, However, there are lots of cool stuff here to get excited about. With PHP 5.4, the new Default Page Charset is now utf-8. There are other features like a callable type hint, support for $this in anonymous functions.With closures, cleaner array syntax, PHP is now looking more like Javascript.

Also read...


  1. Gerard said on :

    Cool stuff. Looking forward to playing with PHP 5.4!

  2. Pingback: PHP 5.4.0 Release Announcement |

Comments are closed.