Migration of Your PHP Application from PHP 5.x to PHP 7.x

October 1, 2021

Last Updated: December 16, 2021

If we review the PHP journey as one of the leading programming languages for website and web applications development, PHP 5 was launched by 2004 and PHP 7 by 2015. Support for PHP 5 was also discontinued by 2019. And PHP 8 is also being launched by Nov, 2020.

It may be irrelevant to discuss this topic for now, as both PHP versions have become old enough. And lots of information already available online about what are the differences between PHP 5 and 7 and what are the improvements or new features available in PHP 7 over PHP 5. There are lots of improvements in terms of performance and security available in PHP 7.

Although, I didn’t find much information from a technical point of view about what steps should be taken for easy migration of PHP 5.x applications successfully in PHP 7.x. There are still lots of old applications running in PHP 5.x which can easily migrate to PHP 7.x with minor but important changes at code level.

Hetarth Consulting is a leading software development company that provides excellent web app development services utilising cutting-edge technologies. We help startups and enterprises to raise their business value by offering innovative software products. If you have a business idea, then feel free to connect with us anytime.

Recently I got a chance to migrate one application written originally in PHP 5.3 to PHP7.4.

Here are some of the steps and tips I would like to share with you.

Make sure “short_open_tag” flag is “On”

Tell PHP whether the short form () of PHP’s open tag should be allowed. if disabled, you must use the long form of the PHP open tag (). This directive does not affect the shorthand

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

This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used.

It means you need to replace all your mysql_* functions with respective mysqli_* functions. You can do it easily with the help of any good IDE(Like Visual Studio Code, Netbeans) by Find and Replace All.

Here is the list of functions regularly used in any PHP 5 applications, which need to be replaced.

mysql_connect => mysqli_connect
mysql_select_db => mysqli_select_db
mysql_query => mysqli_query
mysql_num_rows => mysqli_num_rows
mysql_fetch_row => mysqli_fetch_row
mysql_fetch_array => mysqli_fetch_array
mysql_insert_id => mysqli_insert_id

ereg_* is removed.

This function was DEPRECATED in PHP 5.3.0, and REMOVED in PHP 7.0.0.

Alternatives to this function include,

preg_* (for e.g. preg_match() (with the i (PCRE_CASELESS) modifier)

Use magic method __construct() instead of the class’s name for a constructor

Example (__construct):

class Foo { 
      function __construct(){ 
           //do stuff 

Example (named):

class Foo { 
      function Foo(){ 
          //do stuff 

Old style constructors are DEPRECATED in PHP 7.0 and will be removed in a future version. You should always use __construct() in new code.

Default sql_mode in Mysql 5.7.x

If you have migrated Mysql as well along with PHP, for e.g. Mysql 5.x to 5.7 then you may change your default sql_mode to support old queries, otherwise you may face the below issue.

Reject queries for which the select list, HAVING condition, or ORDER BY list refer to nonaggregated columns that are neither named in the GROUP BY clause nor are functionally dependent on (uniquely determined by) GROUP BY columns.

Here you may remove “ONLY_FULL_GROUP_BY” and may be “STRICT_TRANS_TABLES” from default sql_mode of your mysql.

Solution 1: Remove ONLY_FULL_GROUP_BY from mysql console

mysql > SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));

Solution 2: Remove ONLY_FULL_GROUP_BY from phpmyadmin

  • Open phpmyadmin & select localhost
  • Click on menu Variables & scroll down for sql mode
  • Click on edit button to change the values & remove ONLY_FULL_GROUP_BY & click on save.
Phpmyadmin update sql_mode

In the End…

PHP 7 is going to play a key role in the future of Web Development. Many popular websites developing platforms such as WordPress allow its users to easily upgrade to the new PHP version to experience much better services. Migration from PHP5 to PHP7 will improve your site performance with good quality.

Do you own a website or application in PHP 5? Are you facing any issues in PHP migration? We are ready to help you. Please Contact Us for more information.

A Seasoned full stack IT development and management professional with 14+ years of rich experience in driving business requirements analysis, solution design consulting, business intelligence and software services & products.

Follow on : Linkedin | YouTube

Wondering how to build
an exclusive software product for your business?

Discuss your project needs with our architects.