PHP code changes while upgrading from version 5.1.6 to 5.4.x

From time to time it will be necessary to upgrade php version to the latest version, either to fix bugs or to gain advantages of new features provided in new versions.

Recently a project which is developed using php 5.1.6 version, we have upgraded it to php version 5.4.

Below are the list of minimum required PHP code changes while upgrading from version 5.1.6 to 5.4.x

Issues and solutions :

Removed Function session_is_registered() :
Error :
Fatal error: Call to undefined function session_is_registered()
Solution :
Use
isset($_SESSION[‘userdetails’])
Instead of
session_is_registered(“userdetails”)

 

Removed Function session_register() :
Error :
Fatal error: Call to undefined function session_register()
Solution :
Use
$_SESSION[‘variable’] = ‘value’;
Instead of
$variable = “value”;
session_register(‘variable’);

 

Deprecated Function split() :
Error :
Deprecated: Function split() is deprecated
Solution :
Use
preg_split()
Instead of
split()

This split(‘[/.-]’,$date) will become preg_split(‘/[\/.-]/,$date’)
Example :
$date = “04/30-1973.00”;
list($month, $day, $year, $dd) = preg_split(“/[\/.-]/”, $date);
echo “Month: $month; Day: $day; Year: $year DD:$dd \n”;

 

Deprecated Function ereg() :
Error :
Deprecated: Function ereg() is deprecated
Solution :
Use
preg_match()
Instead of
ereg()
This ereg(‘^string’, $str) Becomes preg_match(‘/^string/’, $str)
This ereg(‘^/string, $str, $array) Becomes preg_match(‘/^\/string/’, $str, $array);

 

Deprecated: eregi()
Error :
Deprecated: Function eregi() is deprecated
Solution :
Use
preg_match()
Instead of
eregi()
This eregi(‘^string’, $str) Becomes preg_match(‘/^string/i’, $str)
This eregi(‘^/string, $str, $array) Becomes preg_match(‘/^\/string/i’, $str, $array);

 

Deprecated Function ereg_replace()
Error :
Deprecated: Function ereg_replace() is deprecated
Solution :
Use
preg_replace()
Instead of
ereg_replace()
This ereg_replace(‘[^A-Za-z0-9_]’, ”, $string) Becomes preg_replace(‘/[^A-Za-z0-9_]/’, ”, $string)

 

Deprecated Function eregi_replace() :
Error :
Deprecated: Function ereg_replace() is deprecated
Solution :
Use
preg_replace()
Instead of
ereg_replace()
This ereg_replace(‘[^A-Za-z0-9_]’, ”, $string) Becomes preg_replace(‘/[^A-Za-z0-9_]/i’, ”, $string)

 

Follow below steps to convert ereg patterns to preg:
Add delimiters(/): ‘pattern’ => ‘/pattern/’
Escape delimiter if it is a part of the pattern: ‘patt/ern’ => ‘/patt\/ern/’
For eregi (case insenstive matching), just add ‘i’ in the end of new pattern(‘/pattern/’).
eregi(case-insensitive matching): ‘pattern’ => ‘/pattern/i’

 

Deprecated Function mysql_escape_string():
Error :
Deprecated: Function mysql_escape_string() is deprecated
Solution :
Use
mysql_real_escape_string()
Instead
mysql_escape_string()

 

Deprecated Function set_magic_quotes_runtime() :
Error :
Deprecated: Function mysql_escape_string() is deprecated
Solution :
Use
ini_set(“magic_quotes_runtime”, 0)
Instead of
set_magic_quotes_runtime(0);

 

Removed Call-time pass-by-reference
Error :
PHP Fatal error: Call-time pass-by-reference has been removed
Solution :
Use
function function_name(&$arg) { } // function declaration
function_name($var); // calling the fucntion
Instead of
function function_name($arg) { } // function declaration
function_name(&$arg); // calling the function

Leave a Reply

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