Common PHP Errors

by Stratus Huo Quan.

Share
|
Homepage | Submit your article | Contact | TOS
More articles on php  

You are here: Categories » Computers and technology » PHP

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.

Leave a comment or ask a question
Total comments: 0

PHP Disclaimer

  • The e-articles directory is not responsible for any and all copyright infringements by writers and authors. If you suspect the information contained by this page for any copyright infringements, please contact us to investigate the issue
How to choose a Programming Language for Web Application - Web Application is an application that is accessed over the Internet and hosted in a browser-controlled environment. Web application is developed using the following programmi (more...)
How PHP useful for business and individual - PHP has become a very reliable and infallible platform for building complex websites. One of the most important and most powerful constituent elements of PHP is its ability to interact with databas (more...)
Benefits of PHP programmers in freelance programming industry - Before you hire a contract programmer to put more features into your website, you need to know which programming language will be helpful for your website as well as affordable for your budget. Sup (more...)
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 firs (more...)
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 (more...)
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 t (more...)
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 i (more...)
PHP mail - In this article, we are going to look at some of the frequently asked questions regarding e-mail and PHP. We will begin by looking at a more fundamental issue: how to actually send an e-mail as (more...)

 
free content
    Copyright © 2006 - 2012 e-articles.info.
The texts, articles and tutorials in the directory are property of their respective owners and authors.