PHP 5.5: Introduction To The New Functionalities

PHP 5.5: Introduction To The New Functionalities

Review of the new features in PHP

by Janeth Kent Date: 02-07-2013 php php5 features programming

PHP 5.5.0 has been released, bringing with it a host of new features and fixes. Here we post a review;

Generators are now available
Generators provide a simple way to iterate through data without having to write a class implementing the Iterator interface.

Just like any other function a generator is defined with the function keyword, but unlike a normal function that just returns a result once, a generator can send back as many results as needed using the yield keyword.

A simple example of using a generator function to print out a positive sequence of integers is shown below.

function xrange($start, $end, $step = 1) {
 for ($i = $start; $i <= $end; $i += $step) {
  yield $i;

echo 'Single digit odd numbers: ';
foreach (xrange(1, 9, 2) as $number) {
 echo "$number ";

echo "\n";

//This would print "Single digit odd numbers:1,3,5,7,9″.

A generator allows you to iterate over a set of data using a foreach loop without needing to build an array in memory. Not having to create an array reduces memory usage and processing time.

For example, using the range() function to generate a sequence between one and one million by calling range(0,1000000) within a foreach loop will create an array of more than 100MB in size, according to the PHP Manual. In comparison, creating a sequence using a generator function will never need to consume more than 1KB.

New password hashing API

The new password hashing API allows you to use one line of code to generate a salted password hash using bcrypt.
For example:

$hash = password_hash($password, PASSWORD_DEFAULT);

password_hash() takes two arguments here, first the password as a string and second a constant setting the encryption algorithm to use.

The password will be automatically salted and can be verified using the following code:

password_verify($password, $hash);

The current default encryption algorithm used is bcrypt, although this is expected to change as new and stronger algorithms are added to PHP.

It is recommended to store the result in a database column that can expand beyond 60 characters

Finally keyword added
The addition of the "finally" keyword refines the way that PHP deals with exception handling.

Like other high level languages PHP allows you to wrap code in a try and catch block. Any exception that is thrown by code within the try block will be passed to code within the catch block to be handled.

The finally keyword allows you to define a block of code, to be placed after the catch block, that will always be executed after the try and catch blocks, regardless of whether an exception was thrown.

The PHP manual gives this example:

function inverse($x) {
 if (!$x) {
  throw new Exception('Division by zero.');
 return 1/$x;
try {
 echo inverse(5) . "\n";
} catch (Exception $e) {
 echo 'Caught exception: ',  $e->getMessage(), "\n";
} finally {
  echo "First finally.\n";

Array and string literal deferencing added

Both array and string literals can now be dereferenced directly to access individual elements and characters.

For example:

echo 'Array dereferencing:';
echo [4,5,6][0];
echo "\n";

//selects the first element in the array to be printed, producing "Array dereferencing:4″.

echo 'String dereferencing:';
echo 'HELLO'[0];

echo "\n";

//selects the first element in the string to be printed, producing "String dereferencing:H".

Easier class name resolution

The class keyword can now be used to retrieve the fully qualified name of a class, including the namespace it sits within.

For example:

namespace NS {
  class ClassName {
  echo ClassName::class;

will print out the both the name of the class and the namespace, producing "NS\ClassName".

Empty() function accepts expressions

The empty() function, used to determine whether a variable is empty or a value equals false, can now be passed an expression and determine whether the variable that expression returns is empty.

For example:

function send_false() {
 return false;

if (empty(send_false())) {
 echo "False value returned.\n";

if (empty(true)) {
 echo "True.\n";

will print "False value returned."

foreach loops now support the list() construct

Values insides nested arrays can now be assigned to variables using a foreach() loop and the list() construct.

List() can be used to easily assign values taken from inside an array to variables, like so:

$animals = array('dog', 'fox');

// Listing all the variables

list($animal1, $animal2) = $animals;

echo "The quick $animal1 jumped over the lazy $animal2\n";

//to produce the familiar, "The quick fox jumped over the lazy dog".

//Now list() can be used with foreach() loops to be assigned values from inside nested arrays, for example:

$array = [
 [10, 20],
 [30, 40],

foreach ($array as list($a, $b)) {
 echo "First: $a; Second: $b\n";

//to produce:

//First: 10, Second: 20
//First: 30, Second: 40

New features added to GD library

PHP's GD extension for creating and manipulating images has gained new capabilities. These include flipping support using the new imageflip() function, advanced cropping support using the imagecrop() and imagecropauto() functions, and WebP read and write support using the imagecreatefromwebp() and imagewebp() functions.

foreach loops now support non-scalar keys

When iterating through an array using a foreach loop, element keys are now able to have a non-scalar value, that is a value other than an integer or a string.
for more about PHP 5.5.0 New features click here

by Janeth Kent Date: 02-07-2013 php php5 features programming hits : 5605  
Janeth Kent

Janeth Kent

Licenciada en Bellas Artes y programadora por pasión. Cuando tengo un rato retoco fotos, edito vídeos y diseño cosas. El resto del tiempo escribo en MA-NO WEB DESIGN AND DEVELOPMENT.


Related Posts

Examine the 10 key PHP functions I use frequently

PHP never ceases to surprise me with its built-in capabilities. These are a few of the functions I find most fascinating.   1. Levenshtein This function uses the Levenshtein algorithm to calculate the…

How to Track Flight Status in real-time using the Flight Tracker API

The Flight Tracker API provides developers with the ability to access real-time flight status, which is extremely useful for integrating historical tracking or live queries of air traffic into your…

What is a JWT token and how does it work?

JWT tokens are a standard used to create application access tokens, enabling user authentication in web applications. Specifically, it follows the RFC 7519 standard. What is a JWT token A JWT token…

PHP - The Singleton Pattern

The Singleton Pattern is one of the GoF (Gang of Four) Patterns. This particular pattern provides a method for limiting the number of instances of an object to just one.…

How to Send Email from an HTML Contact Form

In today’s article we will write about how to make a working form that upon hitting that submit button will be functional and send the email (to you as a…

Interesting and Helpful Google Search Features You’ll Want to Start Using

Google – THE search engine for many internet users. It has been with us since its launch back in 1998 and thanks to its simplicity of use and genius algorithms,…

The State of PHP 8: new features and changes

PHP 8.0 has been released last November 26: let's discover together the main innovations that the new version introduces in this language. PHP is one of the most popular programming languages…

HTTP Cookies: how they work and how to use them

Today we are going to write about the way to store data in a browser, why websites use cookies and how they work in detail. Continue reading to find out how…

The most popular Array Sorting Algorithms In PHP

There are many ways to sort an array in PHP, the easiest being to use the sort() function built into PHP. This sort function is quick but has it's limitations,…

MySQL 8.0 is now fully supported in PHP 7.4

MySQL and PHP is a love story that started long time ago. However the love story with MySQL 8.0 was a bit slower to start… but don’t worry it rules…

ES2019 JavaScript and its new features

ECMAScript 2019 has finished and rolled out new exciting features for developers. The proposals which have been accepted and reached stage 4 by the TC39 committee would be included in…

A roadmap to becoming a web developer in 2019

There are plenty of tutorials online, which won't cost you a cent. If you are sufficiently self-driven and interested, you have no difficulty training yourself. The point to learn coding…