Change from old to new PHP functions

Bulk update your code to run with PHP 7.0+

Migrating from PHP 5.x to PHP 7.x is a hassle when youre dealing with a large website or multiple websites on a server. So, Ive made some improvements to an existing a shell script to recursively modify directories of PHP pages that load in 4 or 5 but will no longer run in PHP 7.

The script will replace all the decommissioned mysql_* functions changing them to mysqli_* and some of the previously deprecated functions – replacing ereg, eregi with preg_match, ereg_match with preg_match, split with preg_split and changing the $HTTP globals to their modern versions.

Save this as a bash file (e.g. php_deprecated_functions_update.sh) and run using: sh php_deprecated_functions_update.sh /home/user/directory ‘\$db’. The second argument is optional and will need something like $db being accessible within classes and functions (such as a global variable). If you haven’t added the global it will display a warning that may assist with manually adding these to classes and functions.

Please always remember backup the files before running this script!

One thought on “Bulk update your code to run with PHP 7.0+”

  1. I’ve added a sanity check on the MySQL resource string (e.g. $db) so it will format this correctly within the script – now you can just use “db” without worrying about the dollar symbol.

Leave a Reply

Your email address will not be published. Required fields are marked *