PHP 5.6 is here!
Posted on: 2014-09-01 | Categories:
PHP
A couple of days ago the PHP Development Team have introduced the new PHP 5.6.0 version. This major release comes with some nice new features, some backward incompatible changes and many improvements. Full list of improvements with details can be found on php.net.
Below i have introduced the main features of PHP 5.6 with examples.
Variadic functions via “…”
This feature with Argument unpacking are my favourites. We don’t need to call func_get_args function any more to get the list of passed parameters. We can use “…” with param name to pass different number of parameters and what is more important they can be combined with “normal” arguments. This works similar to python’s *args.
Sample sum func before:
|
function sum() { $numbers = func_get_args(); $acc = 0; foreach ($numbers as $n) { $acc += $n; } return $acc; } // returns 10 echo sum(1, 2, 3, 4); |
With PHP 5.6:
|
function sum(...$numbers) { $acc = 0; foreach ($numbers as $n) { $acc += $n; } return $acc; } // returns 10 echo sum(1, 2, 3, 4); |
This feature is also related to…
Argument unpacking via “…”
We can also pass Arrays and Traversable objects with “…” operator as function argument. This useful feature is called argument unpacking and will not work with assoc arrays.
Example for sending e-mails (with mail function):
|
// new short array syntax introduced in PHP 5.4 $params = []; $params[] = 'the subject'; $params[] = 'hello'; // additional headers $params[] = 'From: webmaster@example.com' . "\r\n" . 'Reply-To: webmaster@example.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion(); mail("nobody@example.com", ...$params); |
Exponentiation via **
In PHP 5.6 has been added support for exponentiation with operator **:
|
// 9 echo 3 ** 2 ** 1; // 625 echo 5 ** 2 ** 2; // 1 echo 10 ** 0; // 1024 echo 2 ** 10; |
Notice: exponent operator** is RIGHT associative because of:
($a ** $b) ** $c === $a ** ($b * $c)
We can also use shorthand assignment operator: a **=
|
$val = 2; $val **= 5; echo $val; |
Importing functions and constants with use operator
With new PHP version we can now import constants and functions – there are no more limitation to classes.
|
namespace Level7 { const VERSION = 5.1; function ring() { echo "ring ring"; } } namespace { use const Level7\VERSION; use function Level7\ring; echo VERSION."\n"; ring(); } |
Constant scalar expressions
With new PHP it is also possible to provide a scalar expression for constants.
|
const ONE = 1; class Foo { const FOO = 1 + 1; const BAR = 1 << 1; const GREETING = "HELLO"; const BAZ = self::GREETING." WORLD!" public function f($a = ONE + self::FOO) { return $a; } } |
php://input can be now reused
What does it mean? Before PHP 5.6 after reading data form php://input, we can not use it again. Now it’s possible and php://input is available from various levels of application.
Other features worth noting
- default character encoding has been set to UTF-8
- files larger than 2 gigabytes in size are now accepted
- The __debugInfo() magic method has been added
- PHP now includes an interactive debugger called phpdbg implemented as a SAPI module
- GMP objects now support operator overloading and casting to scalar types
Enjoy your new PHP 5.6
This post is also available in:
Polish
Leave a Reply
Your email address will not be published. Required fields are marked *
No comments