Common PHP Errors

written by: Stratus Huo Quan; article published: year 2007, month 11;


In: Categories » Computers and technology » PHP » Common PHP Errors

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.

legal disclaimer

1) Our website is not responsible for the information contained by this article as well for any and all copyright infringements by authors and writers. E-articles is a free information resource. If you suspect this article for any copyright infringements, please read the Terms of service and contact us to investigate the problem.
2) The E-articles directory team is not responsible for inaccuracies, falsehoods, or any other types of misinformation this tutorial may contain and will not be liable for any loss or damage suffered by a user through the user's reliance on the information gained here. Please read the Terms of service

Useful tools and features

Translate this article to...    Send this article to you or to a friend

Link to this article from your page   
If you like this article (tutorial), please link to it from your web page using the information above. Linking to this page, this is the only way to help us improve our service, the same time providing your visitors with a way to improve their online experience.

related articles

1. PHP Date and Time
In this tutorial, we look at commonly asked questions regarding the date function in PHP. How Do I Read the Date or Time from the Server? To read the date or time from the server, you need to use the PHP date function. You pass the date function a string, with special tokens in place of the date parameters you require. When the code is run, the tokens will be replaced with the date or time section that they represent. For example: <?php echo date("d/m/Y"); ?> outputs 11/02/2003 &...

2. PHP Random Password
In some applications, it can be useful to generate a random password, such as setting the user's initial password and then letting the user change it if he or she wishes. The function shown in the following code generates a password with a number of random characters, and you can set the length of the password when you call the function: <?php function randomPassword($length) { $possibleCharacters = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"; $characterLength = strlen($possibleCharac...

3. How to Install PHP into Apache
In this section, we look at how to install PHP into Apache. The first step is to download it from the PHP web site. There are other sources for PHP around the Web, but it is much easier to get it from the source. Downloading PHP PHP is available as a free download from the PHP web site, http://www.php.net/ downloads.php. The file that you need to download is at the top of the page, in the section labeled Complete Source Code. The current file at the time of this writing was for PHP 4.3....

4. Installing PHP with Apache on Windows
We try to install PHP into Apache so it can process PHP pages and static HTML pages. We assume that you have installed and tested Apache. Downloading PHP The first step is to download PHP, which is available from the PHP downloads page at http://www.php.net/downloads.php. Scroll down the downloads page until you find the section labeled Windows Binaries. The current version at the time of this writing is PHP 4.3.0, and the...