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 : 4185  
 
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 END DEVELOPMENT.

 
 
 

Related Posts

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…

10 PHP code snippets to work with dates

Here we have some set of Useful PHP Snippets, which are useful for PHP Developers. In this tutorial we'll show you the 10 PHP date snippets you can use on…

8 Free PHP Books to Read in Summer 2018

In this article, we've listed 8 free PHP books that can help you to learn new approaches to solving problems and keep your skill up to date.   Practical PHP Testing This book…

Best Websites to Learn Coding Online

You know and we know that it’s totally possible to learn to code for free... If you can teach yourself how to write code, you gain a competitive edge over your…

PHP and XMP format: Create a Panorama 360/VR viewer in PHP and A-Frame

The Extensible Metadata Platform (XMP) is an ISO standard, originally created by Adobe Systems Inc., for the creation, processing and interchange of standardized and custom metadata for digital documents and…

PHP: Basic Introduction to Namespaces

History of PHP Namespaces In PHP prior to 5.3 (2009), any class you define lived at the same global level as other classes. Class User, class Contact, class Db they're all together…

PHP 7.2 will be the first Programming Language to add Modern Cryptography to its Standard Library

Last week, the voting phase closed on an RFC to add libsodium to PHP 7.2. The result was unanimous (37 in favor, 0 against). When version 7.2 releases at the end…

Php: How to extend the highlight_string function

PHP has a cool function that automatically highlights PHP code called highlight_string(); Theoretically this could be used to roll your own code highlighting on a site, rather than rely on…

We use our own and third-party cookies to improve our services, compile statistical information and analyze your browsing habits. This allows us to personalize the content we offer and to show you advertisements related to your preferences. By clicking "Accept all" you agree to the storage of cookies on your device to improve website navigation, analyse traffic and assist our marketing activities. You can also select "System Cookies Only" to accept only the cookies required for the website to function, or you can select the cookies you wish to activate by clicking on "settings".

Accept All Only sistem cookies Configuration