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.

file_get_contents("php://input");

ereg_* is removed.

preg_* functions can be used as alternate.

Mssql, sybase_ct

PDO might be alternative extension.

call_user_method();
call_user_method_array();

call_user_func();
call_user_func_array();

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];

print_r($a);

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:

var_dump(1/0);

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

bool(false)

var_dump(0%0);

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

bool(false)

PHP 7:

var_dump(1/0);

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

float(INF)

var_dump(0%0);

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) {

var_dump(current($array));

}

Output:

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

PHP 7:

$array = [0, 1, 2];

foreach ($array as &$val) {

var_dump(current($array));

}

Output:

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:
Example:

define(‘ProgrammingLanguages’, [
‘php',
‘java',
‘ruby'
]);

Class Constant Visibility:
Example:

class ConstantDemoClass
{
const PUBLIC_CONSTANT_FIRE1 = 1;
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.

Example:
session_start([
'cache_limiter' => 'private',
'read_and_close' => true,
]);

New function:
intdiv(): It will handle the exceptions related to integers.
Example:
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
}