|
In this tutorial, we are going to look at some of the common PHP errors that occur and how to solve them.
Parse Errors
A parse error occurs when the format of your PHP code is incorrect. For example, the following code:
<?php
for($i=1;$i<10;$i++){
$output = "Current Iteration: " . $i . "<br>" echo $output;
}
?>
will return an error similar to the following:
Parse error: parse error, unexpected T_ECHO in c:\webserver\test2.php on line 4
This error message is shown because there is a missing semicolon at the end of the third line of the preceding example. The message tells you that PHP wasn't expecting the echo command on line 4. This is because it expected there to be a semicolon at the end of line 3. Most of the time when you get this error, the reason will be a problem with the previous line, such as the missing semicolon as in the previous example.
This error also occurs with braces, {}, for example with the following code:
<?php
for($i=1;$i<10;$i++){
$output = "Current Iteration: " . $i . "<br>";
if($output==5){ echo "This is the fifth iteration";
}
}
}
?>
which will return the error message
Parse error: parse error, unexpected ‘}’ in c:\webserver\test2.php on line 8
This is caused by an extra closing brace, }, on line 8. Although it's simple to see the problem in the preceding short block of code, it can be much harder with complicated code containing many nested loops or if statements, and it's hard to match the opening braces to the closing braces.
To quickly find the problem, you can use the Dreamweaver MX Balance Braces command, which is in the Edit menu. You can place the cursor at a line of code, select the Balance Braces command, and it will highlight all the lines of code for that block from the opening brace to the closing brace. By checking the layers of your code with the Balance Braces command, you can quickly find where there is a missing or extra brace.
Undefined Index or Variable
This section explains what to do if you receive a message on your web page like one of the following:
|
Warning |
Undefined index: action in \home\www\login.php on line 25 |
|
Warning |
Undefined variable: message in home\www\login.php on line 52 |
These messages frequently confuse people, as they look like error messages and can occur even with code that works perfectly. The messages aren't actually errors; instead, they are classed as "notices," which alert you to situations where there isn't actually an error that would stop the code from working. What the message tells you is that the variable mentioned hasn't been specifically defined using a PHP var statement.
As an example, look at the following two blocks of code:
<?php
var $username;
$username = $HTTP_SESSION_VARS['username'];
?>
and:
<?php
$username = $HTTP_SESSION_VARS['username'];
?>
Both blocks of code have identical results, but the second block of code will bring up a message similar to the ones shown previously, because we haven't specifically defined the variable $username. PHP works by defining your variables first, which means it can alert you if you misspell a variable in your code, as the variable will be new and not previously defined.
There are two solutions to this problem. The first is to go back through your code and make sure that every variable is specifically defined. The second option is to stop notices from being displayed, as they're often more trouble than they're worth. You can do this by adding the following code to the top of each page that has the error:
<?php error_reporting (E_ALL &~ E_NOTICE); ?>
This tells PHP to show all error messages and warnings but not to show the notices. To do this permanently, change the error_reporting setting in your php.ini file to the preceding setting.
Headers Already Sent Errors
This is another error message that occurs frequently, especially when using cookies or sessions, and it is similar to the following message:
Cannot modify header information - headers already sent by (output started at /home/web/newtest.php:3) in /home/web/test.php on line 5
The problem is caused by the blank spaces before the header function. The PHP header function is used to redirect the user to another page, which it does by sending commands in the page header to the browser, telling it to go to the new page. However, once a header has been sent to the browser, you can no longer use functions that access the header, such as the header function. If anything is sent to the browser that isn't a special header function, the header is closed and everything else is assumed to be HTML for the web page. So why is this happening with the preceding code?
The problem occurs because there is a blank line (line 2) that is sent to the browser. This causes the header to be closed, so the header function fails, as it can no longer write to the header. However, even if you remove the blank line at line 2, you will get the same error message. This is because there are two spaces at the end of the ?> tag on line 1. These must also be deleted, as again they will be sent to the browser and will close the header. These spaces at the end of lines can be hard to spot because they're invisible!
There is a method you can use, though, to make finding them easier. If you place your cursor at the end of each line and left-click and move the mouse right, any spaces at the end of the line will be highlighted, as shown in the previous screen shot. The spaces can then be removed.
If the code is now tested again, it should work correctly.
This error can also occur if there is output not enclosed in PHP tags in any include files for the page. So if your main page code looks fine and you're still getting the error, the next step will be to check through any include files the page uses. Often, the error is caused by a new line after the final PHP tag in the include file, which must be removed if present.
|