Updating to PHP 7

PHP 7 is here, and it’s the most dramatic update to PHP in over a decade. A revamped engine (Zend Engine 3), numerous new features, and lots of language cleanup mean lots of exciting changes to the language that runs the Web. I am going to show some of the major updates that are visible in PHP 7:

Deprecated Features from PHP 7

Over the last few releases of PHP 5.x, we’ve seen a number of features marked as deprecated, and with PHP 7.0, they have all been removed.

  1. Alternative PHP Tags (ASP Tags, Script Tags)
  2. POSIX-Compatible Regular Expressions (Entire ext/erge extension has been removed)
  3. Multiple Default Cases in Switches – When creating the PHP language spec, a bug was found: you could define multiple default: cases inside a switch, but only the last one would execute, which could lead to potential—hard to find—bugs.
  4. Removal of the Original MySQL Extension- Migrating to Procedural mysqli. The simplest migration path is to the procedural mysqli_ functions, which are part of the ext/mysqli extension.

Uniform Variable Syntax

With Uniform Variable Syntax, all of this inconsistency is fixed, and while it is a backward-incompatible change, it is fairly trivial to change your code to be both forward- and backward-compatible, but it is also a very difficult change to spot.

  1. Consistency Fixes: All variables are evaluated from left to right.

Basic Language Changes

Operators

Null Coalesce Operator

$foo = isset($bar) ? $bar : $baz;
$foo = $bar ?? $baz;

Combined Comparison Operator

Affectionately called the spaceship operator, the combined compari‐ son operator (<=>) is the first trinary operator in PHP.

// Pre Spacefaring^W PHP 7
function order_func_traditional($a, $b) { return($a&lt;$b)?-1:(($a&gt;$b)?1:0);
}
// Post PHP 7
function order_func_spaceship($a, $b) { return $a &lt;=&gt; $b;
}

Constant Arrays

Up until now, constants defined with define() could only contain scalar values. With PHP 7.0, they have been updated to match con‐ stants defined by const, allowing you to set them to constant arrays.

define('DB', [
    'host' =&gt; 'localhost',
    'pass' =&gt; 'newpassqux',
    'db_name' =&gt; 'db_v2'
]);
echo DB['db_name'];

Unpacking Objects Using list()

The list construct will now allow you to unpack objects that implement the \ArrayAccess interface, allowing you to use them just like arrays.

$object = new \ArrayObject(json_decode($json)); 
list($foo, $bar, $baz) = $object;

Type Hints in PHP7

The most polarizing—and exciting—new feature added in PHP 7.0 is without a doubt the addition of scalar type hints.

PHP 7.0 adds the ability to hint on scalar types:

  • bool: Boolean values (i.e., true/false)
  •  float: Floating point numbers (e.g., 4.35)
  •  int: Integer numbers (e.g., 123)
  • string: Strings (e.g., foo)

function demo(bool $a, float $b, int $c, string $d) {
echo $a;
echo $b;
echo $c;
echo $d;
}
demo(true, 4.35, 123, "foo");

Coercive Types

By default, PHP 7.0 will use coercive type hints, which means that it will attempt to convert to the specified type, and, if possible, will do so without complaint. This is the nonstrict option. Coercive types come with some gotchas, most importantly, precision loss.


function sendHttpStatus(int $statusCode, string $message) {
    header('HTTP/1.0 ' .$statusCode. ' ' .$message);
}
sendHttpStatus(404, "File Not Found");
sendHttpStatus("403", "OK");

Return Type Hints

Return hints follow the same rules under coercive or strict type hint settings. With coercive type hints, the return will be coerced to the hinted type when possible, while strict type hints will result in a \TypeError exception on type mismatch.

function divide(int $a, int $b): int {
return $a / $b; }

divide(4, 2);
divide(5, 2);

About the Author

dr.code.skm

I am backend developer with passion in Web Application development using latest technologies like Laravel, PHP7, React, ECMAScript 6 and WordPress. I prefer spending time analyzing big data with Apache Spark. Apart from that, I do photography.