[Previous] [Contents]

Complaints About Headers

We have not introduced the functions header( ) and setcookie( ) in this chapter. Both functions can output HTTP headers that are sent by the web server back to the web browser, and they are used frequently in web database applications. The functions are introduced and discussed in Chapter 5, Chapter 6, Chapter 8, and Chapter 9.

A common problem seen in producing HTTP headers with PHP is the error message beginning:

Warning: Cannot add header information - headers already sent...

Headers can be sent only before any HTML is output, and this includes any whitespace at the top of the file. So, for example, if there is a blank line or single space character before the script open tag <?php, HTML has been output-albeit not very interesting HTML-and the call to header( ) or setcookie( ) reports this error.

It's possible to avoid header problems by altering how PHP buffers data using the output control library functions.

Other Common Problems

The three problem categories we have outlined so far are the most infuriating and common mistakes programmers make in PHP. We outline a few less common and less PHP-specific problems here.

Omitting a semicolon at the end of a statement is usually easy to detect. The PHP interpreter continues to parse the script and, when it reaches a threshold of confusion or exceeds the maximum statement length, it reports an error one or more lines later that indicates a semicolon has been missed. In most cases, this is easy to fix, and the line missing the semicolon is identified in the error message.

In some cases, a missing semicolon can be as hard to identify as a missing closing brace or a missing quotation mark. The following erroneous code is missing a closing brace:

<?
for($x=0; $x<100 ;$x++)
{
  for($y=0; $y<100; $y++) {
    echo "test1";
    for($z=0; $z<100; $z++)
      echo "test2";
}
?>

The error reported is:

Parse error: parse error in bug.php on line 9

Line 9 is the last line of the script, so the nature and cause of the problem aren't immediately clear. However, parse errors that aren't immediately obvious on the reported line in the error message are usually on the line above, or there may be a missing brace or quotation mark.

It takes only a minute or so to identify the missing brace in this example, but more complex functions can take much longer to fix. This highlights the importance of indentation in code and also of avoiding the practice of placing opening braces at the ends of lines. Braces should always be placed on lines of their own.

Missing open and close script tags can cause similar problems, but these are much easier to identify. If an open script tag is missing, it's obvious because some portion of the code-if not all-is displayed in the browser. A missing close tag usually causes a parse error, because the PHP script engine is confused when it tries to parse HTML and interpret it as PHP.

If script source is always displayed and never run, it's likely that Apache is misconfigured. Specifically, it's likely that the AddType directive for processing PHP scripts was not uncommented in the Apache installation process; this seems to be the default in recent RedHat Linux distributions.

Another possible cause of scripts being displayed and not run is that the PHP scripts aren't saved in files ending with the .php suffix. This problem often occurs with legacy PHP3 code, because PHP3 scripts usually use the .php3 suffix. The problem can be corrected by renaming the script files so they end in the .php suffix or by adding an additional AddType directive to the Apache httpd.conf file:

AddType application/x-httpd-php .php3

In some rare cases, a PHP3 script might require minor modifications to run under PHP4.

[Previous] [Contents]