Voice your rage,
Share your rant,
Find angry pals,…
Feel better!

[Internet]

(tips) Beware when you upgrade from PHP 5 to 7

Hi,

I just switched my server to PHP 7. Yes, I know, it's been more than a year since PHP 7 was released, but I kept delaying upgrading.

Finally, I gathered all my courage and proceed to the upgrade. More precisely I switched from PHP 5.6.29 to PHP 7.1.15. I am installing PHP from the source code which is available here : http://www.php.net/downloads.php

I knew that PHP 7 had some incompatibility issues with the PHP 5 branch. So I carefully read the migration documentation : http://php.net/manual/en/migration70.php

In spite of being meticulous, I ended up with several issues which were not well documented online, or not even mentioned at all. So I thought of sharing my experience, in case it can help others.


MySQL

I'll skip quickly the MySQL API. But keep in mind that all "mysql_" functions are no longer available. You need to use "mysqli_" functions or the PDO class. The old MySQL API has been depreciated since a very long time now, but I am sure a lot of PHP codes out there are still using it. So be careful. Also, it's not just a matter of adding an "i" after the "mysql". Some functions are not taking parameters in the same orders. That is another point to pay attention to.


EXTERNAL EXTENSIONS

If you are using PHP extensions, do not forget to verify if they are compatible with PHP 7. Significant things have changed, so old extensions may not be available under PHP 7. Check the PECL site : https://pecl.php.net/ to ensure these extensions are up to date.

If you created your own extension, which is my case, you'll need to upgrade your code too. In that case, something to be VERY careful about, is that when you retrieve parameters passed to your function:

- if you get a string parameter, its length is now of the type "size_t" instead of "long".
- if you get an integer, you need to use a variable of type "zend_long" (instead of "long"😉

I mention these two particular changes, because they will not rise compilated errors or warnings, and on 32bits platforms this will not cause a problem. But if you then try to use it on a 64bits system, this will cause segmentation faults.

You can find a complete list of change here : https://wiki.php.net/phpng-upgrading


NESTED SWITCH STATEMENTS

I came up with a problem which I didn't find reported online. So I don't know if this is a particular case. I had a segmentation fault (segfault) in the "opcache.so" extension. I couldn't find why. And after deep investigation, it came from a nested "switch" statement.

So having nesting a "switch" statement into another one seems to be a problem.


TOO FEW PARAMETERS

Previously, PHP was tolerant when you were passing too few parameters to a function. (which is mistake of programming in fact). Now, it produces a Fatal error. It might sounds obvious, but if you have a LOT of PHP code, you might have missed some parameters without noticing.


Now, if you are using third party PHP codes, like a CMS, message board, etc, check that you have the latest version and that it's compatible with PHP 7. This can be a problem if you are using a discontinued PHP snippet.

All that to say, that this is not entirely smooth to switch from PHP 5 to 7 (there was not version 6 release). This is the primary reason I delayed switching. But once all is correctly working, you should see a nice performance improvement. Personally, my PHP scripts are running 20% faster. They were already optimized and fast, so from a human point of view, the difference might not be really perceptible, but since it's said that Google is taking in consideration the reactivity and speed of sites for its ranking, then it can only be positive.

Hope it can help 🙂

Karl

by: G-1303

no comment, be the first!

June 01, 2017 (2 years ago)

Post a reply or comment:



Browse by Categories:

©2013-2019. WitnessMyRage.com and respective copyright owners | -
0.094b8di/bBzYzmWOAYE1YbuGsJ3uMA79NZFmeNDaaF8YaAUjeplHB5GTfzChukbysvLiVAq3ePD+KZKkA4B8KaDHdxsNo4hV/8k/c4bp1jrX