2.1.4 Upgrading from PHP 3This is NOT the latest copy of this book; click here for the latest version.
Even though PHP 4 has been out for three 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 4.
It is important to note that there was a major change made to external variables in PHP 4.1, and we'll get onto that soon. In order to take full advantage of PHP 5, users upgrading from PHP 3 need to read this chapter also.
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 4 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 see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.
My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!
|