Guide for PHP 5 to PHP 7 Migration

Jul 22, 2019 Surya Narayana Murthy

PHP 7 has been launched with exciting features and drastic performance improvement. There is a drop in memory consumption and many fatal errors are converted to exceptions.

Steps to upgrade from PHP 5 to PHP 7

To upgrade from PHP 5 to PHP 7, there are 3 primary steps. The process listed below will be helpful when the codebase is either medium or large.

  • Choosing an IDE (JetBrains, NetBeans, Eclipse, Visual Studio Code, etc.)
  • Code upgrade process
  • Testing phase

Choosing an IDE

During the upgrade process, it’s hard to know about all the deprecated and removed functions manually; hence, a smart IDE would help solve this issue.

  • Choose a smart IDE and set the environment to PHP 5.6.
  • Open your entire project in IDE and check for the errors and suggestions given by it to refactor the code.
  • If possible, do refactoring in the localhost rather than in the development server connected via SSH.
  • Doing it in the localhost will cut down on the development time and enable the monitoring of deprecated and removed functions.

Code to Upgrade from PHP5 to PHP7

Resolve all errors including notice errors and deprecated errors in PHP 5.4.* to run your code in PHP 7.0 version.

  • Set error reporting to E_ALL as below. Eg: error_reporting(E_ALL ^ E_STRICT);
  • Solve all the errors including notice errors and deprecated errors.
  • Create an abstraction layer for database operations by using PDO extension as PDO supports multiple databases.
  • If the codebase is large, resolve the errors to make it work in PHP 7.0. Post that, you can rewrite your code module by module based on your feasibility.
  • If possible, use the new features to simplify the code.

Testing Phase

  • Maintain the logs for database errors.
  • Create the logs for PHP errors datewise.
  • Create a new testing server i.e. it should be a copy of the production server and release it for a few customers for testing and monitoring for a few months.
  • Continue your testing during that testing period.
  • If everything is okay, you may move it to production.

List of removed extensions and functions in PHP 7.* and alternate extensions and functions in it.

Backward Incompatible Changes in PHP 7.*

Alternate functions or modules or libraries list for this.

mysql extension is removed. We can’t use mysql_* functions. Eg: mysql_query();

Have to use pdo or mysqli functions to handle the database operations.

$HTTP_RAW_POST_DATA is removed.


ereg_* is removed.

preg_* functions can be used as alternate.

Mssql, sybase_ct

PDO might be alternative extension.



split(); function removed in 5.4 itself.

Alternative functions are explode(), str_split();

__autoload() (7.2) function removed.

spl_autoload_register(); can be used as alternate.

$php_errormsg(variable) is removed.

error_get_last(); this function can be used as alternate.

each() (7.2) function is removed


Most of the mcrypt_* functions are deprecated and removed in PHP7.* versions.


Removed short hand syntax.

<? ?>, <?= ?>
<script type=”text/php”></script>

<?php ?> this syntax will be the standard one.

Backward incompatible changes:

  • Changes to variable handling

PHP 5 interpretation: ${$first['light][‘year']}, $foo->{$light['year']}

PHP 7 interpretation: ($$first)['light']['year'], ($first->$light)['year']

  • list() no longer assigns variables in reverse order

list($a[], $a[], $a[]) = [1, 2, 3];


PHP 5 Output: array([0]=>3, [1]=>2, [2]=>1);

PHP 7 Output: array([0]=>1, [1]=>2, [2]=>3);

  • Empty list() assignments have been removed

list() = $a;

list(,,) = $a;

  • Changes to Division By Zero

PHP 5:


Output: Warning: Division by zero in %s on line %d



Warning: Division by zero in %s on line %d


PHP 7:


Warning: Division by zero in %s on line %d



PHP Fatal error: Uncaught DivisionByZeroError: Modulo by zero in %s line %d

  • foreach no longer changes the internal array pointer

PHP 5:

$array = [0, 1, 2];

foreach ($array as &$val) {




int(1), int(2), bool(false)

PHP 7:

$array = [0, 1, 2];

foreach ($array as &$val) {




int(0), int(0), int(0)

  • New objects cannot be assigned by reference

class Test {}

$testobj =& new Test;

PHP 5 Output:

Deprecated: Assigning the return value of new by reference is deprecated in /path/test.php on line 3

PHP 7 Output:

Parse error: syntax error, unexpected 'new' (T_NEW) in /path/test.php on line 3

Some New Features:

Array constants can be defined as below:

define(‘ProgrammingLanguages’, [

Class Constant Visibility:

class ConstantDemoClass
public const PUBLIC_CONSTANT_FIRE2 = 2;
protected const PROTECTED_CONSTANT_FIRE3 = 3;
private const PRIVATE_CONSTANT_FIRE4 = 4;

Session Options:

session_start() now accepts an array of options that override the session configuration directives normally set in php.ini.

'cache_limiter' => 'private',
'read_and_close' => true,

New function:
intdiv(): It will handle the exceptions related to integers.
intdiv ($dividend , $divisor);

list() style will accept the keys also:

$data = [
["id" => 1, "name" => 'Crit1'],
["id" => 2, "name" => ‘Crit2']

// list() style will accept the keys also.
list("id" => $id1, "name" => $name1) = $data[0];

Void Return Type:
We can declare void return type in new version.
function no_return(): void {
// do anything without return

Surya Narayana Murthy

Being a web developer, I love to learn new technologies and stay up-to date with current trends. Apart from my work I like to take apart and assemble electronic devices, travelling and exploring new places at my own pace.

Related Case Studies

Activity coach for the differently-abled built for a world renowned wheelchair manufacturer

Know More

An innovative online shopping entity to a retail store chain

Know More

Application modernization and enhancing user experience for a global logistics firm

Know More