Atlantic.Net Blog

What is: PHP7 – Breaking Changes from PHP5

PHP7 Elephant created by Walker Cahall http://www.waltronic.net/ data-lazy-src=

This article is geared toward readers with at least a working knowledge of PHP or a thorough knowledge of programming.
.

Introduction

PHP is one of those ubiquitous programming languages that underpins many of the most popular web platforms. With the release of PHP7–the first major revision to PHP in years–the governing body has taken the opportunity to add new features, deprecate others, and remove those that were previously deprecated. To make the transition from older versions to PHP7, developers will likely want to familiarize themselves with these changes to take advantage of the increased performance and to learn how these improvements may affect their code.

In this article, we focus on some of the breaking changes from PHP5 to PHP7.

 

Uniform Variable Syntax

Nested variables often resulted in code that was difficult to read as variable assignment was read right-to-left. Variable resolution now happens left-to-right in an effort to make code-writing and -reading a simpler task.

// code // old evaluation // new evaluation
$$atlantic[‘dot’][‘net’] ${$atlantic[‘dot’][‘net’]} ($$atlantic)[‘dot’][‘net’]
$atlantic->$dot[‘net’] $atlantic->{$dot[‘net’]} ($atlantic->$dot)[‘net’]
$atlantic->$dot[‘net’]() $atlantic->{$dot[‘net’]}() ($atlantic->$dot)[‘net’]()
atlantic::$dot[‘net’]() atlantic::{$dot[‘net’]}() (atlantic::$dot)[‘net’]()

.
Old code can achieve the same effect in PHP7 with the use of braces ({}) as in the center column above.
.

Removal of ASP and Script Tags

PHP7 will no longer recognize alternative tags such as <%, <%=, %>, and <script language="php">. PHP7 will only recognize the traditional <?php and ?>.
.

Removal of Deprecated Extensions and Features

The ereg extension and all ereg_* associated functions have been removed (deprecated in PHP 5.3) and replaced by the PCRE extension: preg_*.

The MySQL function (deprecated since PHP 5.5) has also been changed from mysql_* to mysqli_*.

The assignment of new by reference will now produce an error.

.

Multiple Default Cases Disallowed in a Switch

In what might qualify as an outlier intended usage, switch will no longer accept multiple default statements. In earlier PHP versions, switch would have evaluated the final default in the example below.

switch ($expr) {
default:
neverExecuted();
break;
default:
executed();
}

But in PHP7, the result of this code will be: Fatal error: Switch statements may only contain one default
.

PHP4 Constructors Are Now Deprecated

PHP4 constructors are methods that have the same name as the class they are defined in. PHP7 will emit an E_DEPRECATED when encountering a PHP4 constructor (in PHP8 this method will no longer be recognized as a constructor). This change may only produce problems with custom error handlers.
.

Redefinition of Function Parameter when the Name is Duplicated

In a fashion similar to the behavior of multiple defaults in a switch statement (see above), using duplicate parameter names within a function definition would return the final value. In PHP7, this redefinition attempt will produce an error.

function foo($bar, $bar) {....} // Return Fatal error: Redefinition of parameter $bar in /duplicate_names.php

.

Hex String No Longer Recognized as Numerical

To clear up inconsistent handling of hexadecimal numbers, PHP7 will no longer recognize a hex string as a number. This change will primarily affect the is_numeric() function and the mathematical operators (e.g., ==, +, -, *, /, %, **, ++, and –)

// In PHP5
var_dump(is_numeric('0x16a'));
// Return bool(true)

// In PHP7
var_dump(is_numeric('0x16a'));
// Return bool(false)

.

Invalid Octal Error

An invalid octal literal (such as one containing a non-octal digit) will now produce an error, instead of making a best-effort to render the value.

 // In PHP5
$octal = 0149;
echo $octal; // Display 12 (rendering octal value 0014)

// In PHP7
$octal = 0148;
echo $octal;
// Return: Parse Error: Invalid numeric literal...

.

Division by Zero Results Change

PHP7 adjusts how it reacts to situations in which it attempts to divide a value by zero. In previous versions, division by zero resulted in a boolean false. Now, dividing (/) by zero will return a float INF or NAN; using a zero divisor in a modulus (%) operation will return an error.

var_dump(5/0); // will result in float(INF)
var_dump(0/0); // will result in float(NAN)

var_dump(5%0); // will produce DivisionByZeroError

 

 

Thank you for reading, and good luck as you migrate your code to PHP7. Check out our how-to’s on installing PHP7  and try it yourself (CentOSDebian, Ubuntu).  For a more comprehensive dive into all the changes and the reasons behind those changes, see the PHP7 RFC list.   Altantic.Net offers many cloud hosting solutions, managed cloud hosting, one-click application installs and many other hosting solutions.

Learn more about our reliable dedicated server hosting solutions..

Get a $250 Credit and Access to Our Free Tier!

Free Tier includes:
G3.2GB Cloud VPS a Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year