PHP 5.5  with certain advanced features are delivered contemparorily. We are going to discuss about the major aspects enclosed in PHP5.5.

Important Features

  1. Adding Generators
  2. “Finally” keyword
  3. Password Hashing API
  4. Opcode cache extension
  5. Array dereferencing
  6. Supports random expressions

Adding Generators

  • In this we can use a simple iterator without implementing “iterator” interface.
  • Iterate results without using array in memory.
  • It break records up to the memory limit.
  • It also saves our time
  • Another feature introduces the keyword ‘yield’.

For example: shows the numbers from 1 to 8 using array.

[sh lang=”php”]

<?php

function myrange($start,$limit)

{

$array=array();

for($i=$start;$i<=$limit;$i++)

{

$array[]=$i;

}

return $array;

}

foreach (myrange(1,8) as $i)

{

echo $i;

}

?>

[/sh]

Output

12345678

We can also create the same example using the keyword ‘yield’ instead of an array. The result will be same as above.

[sh lang=”php”]

<?php

 

function myrange($start,$limit)

{

 

for($i= $start;$i<= $limit; $i++)

{

yield $i;

}

 

}

foreach(myrange(1,10) as $i)

{

echo $i;

}

?>

[/sh]

 

Output

12345678

Note: The above code will only work with PHP 5.5,  if your are not using php 5.5 for your project it will show error.

Finally

  • Just like in Java we can use ‘Finally’ keyword.
  • Mostly concerns with resource handling to prevent resource leaking.
  • Finally will be executed whether the exception caught or not.

Syntax

[sh lang=”php”]

<?php

try

{

}

catch($exc)

{

throw $exc;

}

finally

{

}

?>

[/sh]

Opcode cache extension

  • It provides faster PHP execution through opcode caching and optimization
  • Save precompiled script bytecodes in communal memory.
  • There is also an APC (Alternative PHP Cache).
  • Zend OpCache is now bundled with PHP5.2,PHP5.3,PHP5.4.

Password Hashing

  • password_hash() develops a new password hash using a stable one-way hashing algorithm.
  • password_hash() is adaptable with crypt(). Therefore, password hashes by crypt() can be used with password_hash().
  •  It can set the algorithm,the cost and the salt.
  • The value given back will hold the algorithm,the cost,the salt and hash.
  • It demands only one storage.
  • password_verify () yields the plain password and the hash result.

Example for password_hash()

[sh lang=”php”]

<?php

/**

* We just want to hash our password using the current DEFAULT algorithm.

* This is presently BCRYPT, and will produce a 60 character result.

*

* Beware that DEFAULT may change over time, so you would want to prepare

* By allowing your storage to expand past 60 characters (255 would be good)

*/

echo password_hash(“seamedia”, PASSWORD_DEFAULT).”\n”;

?>

[/sh]

Output

$2y$10$xk9OBG2GOcUnFuOPKFXjR./7nhyrgT2k6B5aMhwscGLr/i.qFTyW2

Example for password_hash() setting cost manually

[sh lang=”php”]


<?php

/**

* In this case, we want to increase the default cost for BCRYPT to 12.

* Note that we also switched to BCRYPT, which will always be 60 characters.

*/

$options = [

‘cost’ => 12,

];

echo password_hash(“seamedia”, PASSWORD_BCRYPT, $options).”\n”;

?>[/sh]

Output

$2y$12$j2OmMgVdkHWm3TXYmFee3eCIi.fy1SLJoKqoGEZGdZ/uXBmr5fx76

Example for password_hash() setting salt manually

[sh lang=”php”]

<?php

/**

* Note that the salt here is randomly generated.

* Never use a static salt or one that is not randomly generated.

*

* For the VAST majority of use-cases, let password_hash generate the salt randomly for you

*/

$options = [

‘cost’ => 11,

‘salt’ => mcrypt_create_iv(22, MCRYPT_DEV_URANDOM),

];

echo password_hash(“seamedia”, PASSWORD_BCRYPT, $options).”\n”;

?>

[/sh]

Output

$2y$11$iOD3C4BedHo6bZUBUv1dXuh.NHmpFyB62U4x3cGZLyHJxmG3iCENm

Example for password_hash() finding a good cost

[sh lang=”php”]

<?php

/**

* This code will benchmark your server to determine how high of a cost you can

* afford. You want to set the highest cost that you can without slowing down

* you server too much. 10 is a good baseline, and more is good if your servers

* are fast enough.

*/

$timeTarget = 0.2;

 

$cost = 9;

do {

$cost++;

$start = microtime(true);

password_hash(“test”, PASSWORD_BCRYPT, [“cost” => $cost]);

$end = microtime(true);

} while (($end – $start) < $timeTarget);

 

echo “Appropriate Cost Found: ” . $cost . “\n”;

?>

[/sh]

 

Output

Appropriate Cost Found: 12

 Array dereferencing

Array and string can now be had referenced directly to access individual elements and characters: For example:

[sh lang=”php”]


<?php

echo ‘Array dereferencing: ‘;

echo [1, 2, 3][1];

echo “\n”;

 

echo ‘String dereferencing: ‘;

echo ‘PHP'[1];

echo “\n”;

?>

[/sh]

Output

Array dereferencing: 2

String dereferencing: H

Supports random expressions

Passing a random expression instead of a variable to empty() is now supported.

For example:

[sh lang=”php”]


<?php

function always_false()

{

return false;

}

 

if (empty(always_false()))

{

echo “PHP5.5 new features \n”;

}

 

if (empty(true))

{

echo “No values \n”;

}

?>

[/sh]

 

Output

PHP5.5 new features

Download PHP5.5 from http://php.net/downloads.php