Upgrading from PHP 3

This chapter has been left in for historical reference. Surely no one is still running PHP 3...?

Even though PHP 4 has been out for years now, some people have yet to upgrade. If this is you, read on - I will be looking at how to take the sting out of upgrading to the latest release of PHP.

To start with, one of my favourite changes made in PHP 4 was the new variable substitution code within strings. All too often in PHP 3, I had found myself writing:

print "Array item 3 is " . $myarray[2] . " currently";

This is not particularly easy to read, and, thankfully, was improved in PHP 4. You can now put braces (the { and } symbols) around arrays and objects inside strings to have their variable correctly substituted. Thus:

print "Array item 3 is {$myarray[2]} currently";

More on how arrays work later. As mentioned already, the execution paradigm has changed between version 3 and 4. Owing to the fact that PHP 4 now compiles everything before executing anything, a new requirement was introduced which enforces syntactically complete files for source code.

It used to be the case in PHP 3 that you could start a while loop in one file and end it in another by using include(). This trick no longer works - all files you include() or require() must be syntactically correct on their own merits.

You will also find that no extensions written for PHP 3 will work with PHP 4, even if you recompile them. This is a side effect of PHP 4 having the Zend Engine at its core, and actually requires some re-writing of the extension. Don't worry, though - all the core extensions were re-written long ago.

Perhaps a very likely culprit for a code-breaking change was the behaviour of setcookie(). This was notorious to users of PHP 3, as, for one reason or another, PHP 3 would send the HTTP set-cookie headers in the reverse order of what you specified in your code. This has been rectified in PHP 4 - cookies are sent in the order you request them to be sent.

Finally, you need to be wary of the added functionality available in PHP 4. When I made the switch, many of my pages immediately stopped working simply because PHP 4 had a function in_array(), whereas PHP 3 did not. As in_array() is incredibly useful (it returns TRUE if it finds a given item in an array), I had written my own in_array() implementation for PHP 3, and PHP 4 complained that I was trying to redefine a function.

If you have the luxury of a test machine, I would recommend you make the switch to PHP 5 there, and see the results - it is quite likely the biggest incompatibilities will come about because of the change made in PHP 4.1. This change is discussed in the next chapter, and you will need to read it in order to upgrade smoothly from PHP 3.

 

Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: Upgrading from PHP 4 >>

Previous chapter: Current release

Jump to:

 

Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.