PHP 3 and 4 Incompatibilities: A Closer Look Continued
Inc. #4 - Scope of break and continue
In PHP 4, the scope of a break or continue statement in an include()'d file or an eval()'d string is local to the file or string itself. Using such a statement to affect a loop outside the file or string won't work, and will generate an error.
In PHP 3, the scope was not limited in this way. With PHP 3, you could, for example, have code like this on a web page:
$files = return_list_of_relevant_files ($somevar);
for ($count = 0; $count < count($files); $count++) {
include ("$files[$count].inc"); /* pull in contents of file */
process_file ($files[$count]); /* maybe log the fact that the file was accessed? */
}
and in one of the included files have the text you want to display, plus a continue statement:
... text to be included ...
<?PHP continue; ?>
Why would you want to do this? Perhaps for certain files/cases, you don't need to process the file (maybe you log the fact that the file was accessed in most cases, and use the continue statement where logging isn't needed).
To get a similar result in PHP 4, you could set a variable instead of using continue:
... text to be included ...
<?PHP $continue_var = 1; ?>
and run the process_file function only if $continue_var is false:
$files = return_list_of_relevant_files ($somevar);
for ($count = 0; $count < count($files); $count++) {
$continue_var = 0;
include ("$files[$count].inc"); /* pull in contents of file */
if (! $continue_var) {
process_file ($files[$count]);
}
}
Inc. #5 - unset() is no longer a function
Since unset() was never documented as a function (so says the PHP team), nobody should have ever used it. Theoretically, though, you could have been using unset for something like the following case, where you need a variable to be unset before going through a loop, and wanted to use unset's return-value of 1 as a side-effect:
while (unset ($x)) {
if ($y) {
$x = set_x_value ($y);
}
if (isset ($x)) {
process_x_value ($x);
}
if (... some break-condition ...)
break;
}
}
To avoid getting a parse error, you can put the unset statement inside the loop, and put an always-true value in to keep the loop going:
while (1) {
unset ($x);
...
The Rest of 'Em
Here's a list of the remanining incompatibilities listed on the PHP site. I'm not going into as much detail with these, because - well, they seem a bit less interesting to me.
- unset() now breaks the association between a locally scoped variable and one that is globally scoped if the reference is made using the "global" keyword.
- you can no longer use return to return from a require()'d file (though you can from an include()'d file)
- you can't use PHP 3 dynamic extensions with PHP 4 (which you can get around by using the PHP 4 dynamic extensions)
- static variable and class member initializers will now only accept scalar values
- the function short_tags() no longer works (though an alternative is forthcoming)
If you've got a large set of PHP 3 code to migrate, it'll definitely pay off to test your code after upgrading to PHP 4. Don't let the incompatibilities keep you from upgrading; gaining the speed and reliability of PHP 4 will be worth the hassle.
Edward Piou is an ahref.com producer and runs ep Productions, Inc., a development company based in the Washington, D.C. area. |