r/PHPhelp • • 26d ago

Patching code full of old syntax/functions

Hello,

I am patching some php code of some kind of DB web integration and the code is quite old. After giving it into phpstorm to check for problems, it showed dozens of syntax errors (mostly curly braces for accessing array elements, no big deal in changing them to squared ones) and whole functions that arent supported anymore. Mostly things as MYSQL_BOTH, MYSQL_NUM, etc. Is there a nice way to fix those issues, maybe even some kind of software, or do I need to manually rewrite those passages w/o the functions?

8 Upvotes

12 comments sorted by

View all comments

3

u/thejester1324 26d ago

Two separate problems with different fixes.

The $row{0} offset syntax was removed in PHP 8.0 (it's in the migration guide), so that part is a search and replace to $row[0]. Don't do a blind replace on every {, though. Interpolation like {$row['name']} inside double quotes is still valid, so only the $var{...} form needs changing.

The mysql_* functions have been gone since PHP 7.0. I wouldn't count on Rector for those, so that part is hand work. The thing to watch is argument order: mysql_query($sql, $link) becomes mysqli_query($link, $sql), with the connection first. MYSQL_BOTH and MYSQL_NUM become MYSQLI_BOTH and MYSQLI_NUM as the mode argument to mysqli_fetch_array.

Grep for mysql_ and convert one call at a time so you don't swap arguments by accident.