Quick-fix PHP 5.3 Compatibility Issues

On December 21, 2010, in Computing, Hacks, Software, by Anthony Lake
PHP Logo

Fixing PHP 5.3 Compatibility Issues Fast

If you recently updated to PHP 5.3 and are getting a lot of undeclared variable errors, warning messages, problematic global variables, or nothing but a blank screen appears where your web site is supposed to be, here’s a few “quick-fixes”. Please note that it’s up to you to get your code up to snuff… but this may get your production sites running until you get everything squared away.

Edit the php.ini File

First of all, generally, your solution is mainly in the php.ini file. This is probably a relief because your break-fix time is probably going to be less than you feared if you’re reading this.

If you’re not seeing any errors at all but just the white screen of death hope (hehe), edit php.ini to make sure display_errors is on.

display_errors = On

Now, at this point, if you restarted Apache, you may be surprised at the number of  “Undeclared Variable” warnings you see. Not to get on the soapbox, but some of the older coders out there are surely going to remember that one of the great things about early PHP was the fact that you didn’t have to explicitly cast or DIM any variables… you just assigned and used them. Now, the tide has turned the other way and we’re back to 1985;  variables need to be declared first or a warning will be thrown. We can control that for now by setting the error reporting level.

If you want to see every parser complaint, just include in your php.ini file

error_reporting  =  E_ALL

Otherwise, you probably want to be less restrictive until you get those productions servers brought up to the standards they should have been at when the code was written. Let’s go with the very loose:

error_reporting  =  E_ALL & ~E_NOTICE & ~E_WARNING

That will quiet the default php 5.3 error reporting down  until you can get the code more 5.3 compliant.

Edit Your Code

Make sure there are no error reporting levels set directly in the  code which might override the settings in the php.ini file. Look for

ini_set('display_errors', FALSE);
error_reporting(E_ALL & ~E_NOTICE);

or similar lines of code and alter them/delete them as necessary for now.

Global Variables in PHP 5.3

You may be having some issues with global variables in 5.3. I didn’t immediately find a way to get around this with the ini file, so at this point the apparent not-so-wonderful truth is  that if you are supporting code that relies on a global variable, you’re going to have to declare that variable as global OUTSIDE any function where the global is accessed first. Therefore, we need to make sure you have something like the following pseudo-code for every variable you want to treat globally:

global myVar;
myVar = 5;
function myFunction()
     global myVar=3;

Obviously, this may not be a lengthy recovery process depending on how cleverly crafted the code you’re working with is. It could take a while- but it’s doable.

Restart Apache

I’m including this subsection because I have seen a number of articles where people have done everything right but are still not getting the expected result. I don’t want to overlook the obvious but… restart Apache. Now revisit your site. There may still be errors with that production code from 2003, but I bet it’s looking a lot more managable now!

Good luck!


Leave a Reply

Switch to our mobile site