Feb 2, 2017

f Comment

How do you migrate or upgrade from PHP 5.X to PHP 7?

Amazon If you are forced to upgrade from PHP 5.X to PHP 7, you are in for a surprise. There are PHP 5 functions that are deprecated in PHP 7 and therefore will cause errors when running in the PHP 7 environment. So you have two options:

#1. Downgrade your OS version to any version compatible with PHP 5.X. (e.g. Ubuntu 14.04).

This is not impossible. For example, if you use a cloud server like Digital Ocean or Amazon EC2, they allow you to select a specific version of an OS image. It'll be a while before they disallow OS versions that are compatible with PHP 5.X.

#2. Update all your PHP files by modifying deprecated PHP 5 functions to valid PHP 7 functions.

This option makes it possible for you to enjoy the new features that come with PHP 7, but you must go through the pain of going through each PHP file and updating them to make sure they are compatible with PHP 7.

How do you do option #2?

One of the biggest changes from PHP 5 to PHP 7 is that mysql* functions are no longer supported by PHP 7; you must use their counterparts which look like mysqli* by referring to the PHP manual. For example,

mysql_pconnect($dbHost, $dbUser, $dbPw);

becomes

mysqli_connect('p:'.$dbHost, $dbUser, $dbPw);

And

$result = mysql_query($sql, $connection);

becomes

$result = mysqli_query($connection, $sql);

As you can see most changes are simple. You change the name from mysql* to mysqli* and switch the parameters and you are done. However, there are mysql* functions that don't have counterpart mysqli* functions such as:

string mysql_result ( resource $result , int $row [, mixed $field = 0 ] )

In this case, you'll need to write your own PHP 7 function such as:
public function mysqli_result_custom($res, $row, $field=0) {
  $res->data_seek($row);
  $datarow = $res->fetch_array();
  return $datarow[$field];
}
In summary it's not hard to migrate from PHP 5 functions to PHP 7 functions. It just takes a bit of time and effort to make the transition from PHP 5 to PHP 7. But read on to see what the problem is.

So What's the problem with migrating to PHP 7?

In my case, after I've made the transition to PHP 7, my PHP web application wasn't working. It kept telling the web browser to redirect to itself and therefore causing an infinite redirect loop. There were no errors in the log. It turns out PHP 7 is still in beta testing and it's just not stable enough as of now.

So my best advice to you is DO NOT UPGRADE TO PHP 7 until it's more stable.

Questions? Let me know!
Please leave a comment here!
One Minute Information - by Michael Wen
ADVERTISING WITH US - Direct your advertising requests to Michael