The All New PHP7
The Language which Powers 80% of the web doubles its speed in performance
“PHP 7 is a landmark achievement in the history of PHP and one of the most exciting moments in my career as a web developer. When the language that powers the majority of the web doubles in speed, it’s something to get excited about.” – Taylor Otwell, Laravel Founder
PHP 7 is released with numerous improvements and copious features. In this blog I have given a gist of the newly added features.
1. Return type declarations
We can now give return type declaration in PHP7
function sum($a, $b): float
return $a + $b;
// Note it returns a float
2. Null coalesce operator
Null coalesce operator “??” is added which is similar to isset(). It returns the first operand if it exists and is not NULL otherwise it returns the second operand. One additional feature is that this operator can also used as a chain.
$usertype = $_GET[‘usertype’] ?? ‘Member’;
$usertype = isset($_GET[‘usertype’]) ? $_GET[‘usertype’] : ‘Member’;
$usertype = $_GET[‘usertype’] ?? $_POST[‘usertype’] ?? ‘Member’;
3. Scalar type declarations
The scalar type of declaration which can help us to use declare and use n number of variables during runtime. It comes in two flavours coercive and strict
function sum(int …$ints)
var_dump(sum(8, ‘1’, 2.1));
4. Constant arrays
We can define array constants now as below
5. Integer division
PHP 7 has introduced new function intdiv() to perform integer division
PHP 7 has a also concentrated to provide better security when unserializing objects on untrusted data which prevents code injections. Now developers can whitelist classes.
// converts all objects into incomplete classes
$data = unserialize($foo, [“classes_allowed” => false]);
// converts Class1 and Class2 as complete classes and all other as incomplete
$data = unserialize($foo, [“classes_allowed” => [“Class1″, “Class2”]]);
// Converts all objects into completed classes
$data = unserialize($foo, [“classes_allowed” => true]);
Try this and get started with the new features, more flavours of PHP will be added in the next blog.
PHP Team Lead,