What’s new in PHP 5.4.x ?

Categorized under: Web Development

Hey folks! Did you hear the news? PHP 5.4.0 has just been released and it brings a few important updates that we will discuss about today.

So let’s talk about the great features that have been implemented in this version.

I guess every PHP programmer wrote at least one uploading script and you know that is absolutely impossible to check the uploading progress without using flash or other similar techniques. Fortunately PHP 5.4 is here to show you the light. Using sessions, you can now check the upload progress at any time. How does it work ?

Prepare your HTML by inserting the following line in your code inside the form:

<input type="hidden" name="<?php echo ini_get("session.upload_progress.name"); ?>" value="123" />

Value of the field is arbitrarily chosen, put whatever (valid value) you want there.
After you hit the Upload button, you can periodically send and XHR (ajax) request to the server and ask him how much do you have to wait. Server response will be something like this

$_SESSION["upload_progress_123"] = array(
 "start_time" => 1234567890,   // The request time
 "content_length" => 57343257, // POST content length
 "bytes_processed" => 453489,  // Amount of bytes received and processed
 "done" => false,              // true when the POST handler has finished, successfully or not
 "files" => array(
  0 => array(
   "field_name" => "file1",       // Name of the <input/> field
   // The following 3 elements equals those in $_FILES
   "name" => "foo.avi",
   "tmp_name" => "/tmp/phpxxxxxx",
   "error" => 0,
   "done" => true,                // True when the POST handler has finished handling this file
   "start_time" => 1234567890,    // When this file has started to be processed
   "bytes_processed" => 57343250, // Amount of bytes received and processed for this file
  // An other file, not finished uploading, in the same request
  1 => array(
   "field_name" => "file2",
   "name" => "bar.avi",
   "tmp_name" => NULL,
   "error" => 0,
   "done" => false,
   "start_time" => 1234567899,
   "bytes_processed" => 54554,

Source: http://php.net

This brings uploading files with php and html to a new level. A complete uploading script with progress will be published in a future post.

The second great feature implemented in PHP 5.4.0 is how you work with arrays. Before PHP 5.4, the only way you could create an array was by calling
$arr = array(); or $arr = array(1, 2, 3);
But from now on we can use this new built it syntax
$arr = []; or $arr = [1, 2, 3, 4];
Most of us are already used with this syntax from JavaScript, right? But wait, there is more! Have you ever used the following code? (I did!)

$arr = explode(',', $list);
$arr = $arr[0];

I haven’t found a better solution for this, except for creating a function that I was using it like

$arr = array_get(explode(',', $list), 0);

From now on, thanks to PHP 5.4 you can use the built in syntax:

$arr = explode(',', $list)[0];

Of course there are a lot more features in PHP 5.4.x, like:

  • Support for traits
  • Closures now support $this.
  • Class member access on instantiation, e.g. (new Foo)->bar().
  • Class::{expr}() syntax support
  • Binary number format, e.g. 0b001001101.

The last feature I want to talk about is that <?= and <? are now available, regardless of the short_open_tag. I allways prefered the long form (<?php echo and <?php) because in the past I used some servers that didn’t support short open tags and I had to fix all my code. And I still recommend you to use the long form in order to avoid code rewriting.

In the next post I’ll talk about removing Magic quotes and how this change affects our code. So keep in touch.

Are you happy with these features?