PLEASE note: These pages are here solely for historic purposes. New articles have not been written since 2001; many links in the index are broken; and most ahref.com email addresses will now bounce. Try visiting ep Productions, Incorporated, the web programming and development company behind this site.

Tip: Looking for industry conferences? Try the conference calendar.

web index ahref.com: a community space for web developers------ -----
IndexToolsCareersTalk
ahref.com > Guides > Technology
Technology Guide

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.

<<< to the first page of the article
or jump to a topic:

Zero Is Empty
Setting Cookies and Quoting {$
Break/Continue, Unset, and the Rest

view a printable version of this article


To suggest a topic, please email info@ahref.com.

 


HOME ||| ABOUT AHREF.COM ||| ADVERTISE ||| FEEDBACK ||| SEARCH THIS SITE ||| CONTRIBUTE

This site © 1998-1999 ep Productions, Inc. Text of any articles is copyright of the author. All rights reserved. Terms of use.