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 a specific web site? You can search the index.

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. #2 - in PHP 4, multiple calls to setcookie() are performed in the order called

This setcookie() behavior is opposite that of PHP 3, in which calls to setcookie() were executed in reverse call order. This will be most important when you want to make sure you've deleted one cookie before inserting another. Say, for example, you've already (or might have already) set 19 cookies for a user from one particular server. If you try to set 2 more, theoretically, the user's browser may delete the oldest cookie you've given it (according to Netscape's cookie specification) rather than hold more than 20 cookies from your server. If you'd rather clear out some specific cookies before inserting, you could do this in PHP 3:

  setcookie ("NewCookie2", $cookie_val2,time()+3600);
  setcookie ("NewCookie1", $cookie_val1,time()+3600);
  setcookie ("OldCookie2");
  setcookie ("OldCookie1");

which would remove OldCookie1 (user now has 18 cookies), then OldCookie2 (17), then insert NewCookie1 (18), then NewCookie2 (19).

In PHP 4, you'd need to send the commands in reverse (or correct, depending on how you look at it) order for the same effect:

  setcookie ("OldCookie1");
  setcookie ("OldCookie2");
  setcookie ("NewCookie1", $cookie_val1,time()+3600);
  setcookie ("NewCookie2", $cookie_val2,time()+3600);

Probably not a problem, unless you and your coworkers are going crazy with setting cookies...

Inc. #3 - {$ is not supported within encapsulated strings

This means that you can't use statements like:
  print "{$x}";
    or
  print "{$ }";

when you're trying to print a "{" followed by a variable or just a dollar sign.

The content you're printing to a user's browser usually won't use bracketed variables; but if you generate your cascading style sheets or javascript functions with PHP you might have problems. For example, if you change the color of H1 tags based on some user-supplied or other variable:

  <style type="text/css">
  <!--
  H1
  <?PHP
    Function user_css ($some_var) {
      if ($some_var == 'r') {
        return "color: red";
      }
      else {
        return "color: blue";
      }
    }
    $x = user_css ($some_var);
    print "{$x}";
  ?>
  -->
  </style>

the print statement won't work correctly. One fix for this type of problem is to put the curly braces inside the variable; otherwise, changing the print line to:
  print "\{$x}";

as the PHP team suggests, or alternatively
  print "{" . $x . "}";

should work.
continue reading >>>
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.