Top Ten Troubleshooting Tips While Writing PHP Script

Updated on     Kisan Patel

This post will show you ten of the most common errors and how to avoid them while writing PHP script.

Missing Semicolons

Every PHP statement ends with a semicolon (;). PHP doesn’t stop reading a statement until it reaches a semicolon. If you leave out the semicolon at the end of a line, PHP continues reading the statement on the following line. For example, consider the following statement:

$test = 1
echo $test;

These statements don’t make sense to PHP; it reads the two lines as one statement, so it complains with an error message, such as the following:

Parse error: parse error in c:\test.php on line 2

This is a very common error. Before you know it, you’ll be writing your home address with semicolons at the end of each line.

Not Enough Equal Signs

In a comparison statement, in which you ask whether two values are equal, you need two equal signs in a row. Using one equal sign is a common mistake.

It’s a perfectly reasonable error because you have been using one equal sign to mean equal since the first grade when you learned that 2 + 2 = 4. This is a difficult mistake to recognize because it doesn’t cause an error message. It just makes your script do odd things, like infinite loops or if blocks that never execute. I am continually amazed at how long I can stare at

$test = 0;
while ( $test = 0 )
{
   $test++;
}

and not see why it’s looping endlessly.

Misspelled Variable Names

This is another PHP gotcha that doesn’t result in an error message, just odd script behavior. If you misspell a variable name, PHP considers it a new variable and does what you ask it to do. Here’s another clever way to write an infinite loop:

$test = 0;
while ( $test == 0 )
{
   $Test++;
}

Remember, to PHP, test is not the same as Test.

Missing Dollar Signs

A missing dollar sign in a variable name is really hard to see, but at least it usually results in an error message so that you know where to look for the problem. It usually results in the old familiar parse error:

Parse error: parse error in test.php on line 7

Troubling Quotes

You can have too many, too few, or the wrong kind of quotes. You have too many when you put quotes inside of quotes, such as this example:

$test = “<table width=”100%”>”;

PHP sees the second double quote (“) — before 100 — as the ending double quote (“) and reads the 1 as an instruction, which makes no sense. Another parse error. The line must be either

$test = “<table width=’100%’>”;

or

$test = “<table width=\”100%\”>”;

You have too few quotes when you forget to end a quoted string, such as

$test = “<table width=’100%’>;

PHP continues reading the lines as part of the quoted string until it encounters another double quote (“), which may not occur for several lines. This is one occasion when the parse error that points to where PHP got confused is not pointing to the actual error. The actual error occurred some lines previously, when you forgot to end the string.

You have the wrong kind of quotes when you use a single quote (‘) when you meant a double quote (“) or vice-versa. The difference between single and double quotes is sometimes important

Invisible Output

Some statements, such as the header statement, must execute before the script produces any output. If you try to use such statements after sending output, they fail. The following statements fail because the header message is not the first output:

<html>
<?php
header(“Location: http://company.com”);
?>

<html> is not in a PHP section and is therefore sent as HTML output. The following statements work:

<?php
header(“Location: http://company.com”);
?>
<html>

The following statements fail:

<?php
header(“Location: http://company.com”);
?>
<html>

It’s not easy to see, but there’s one, single blank space before the opening PHP tag. The blank space is output to the browser, although the resulting Web page looks empty. Therefore, the header statement fails because there is output before it. This is a common mistake and is difficult to spot.

Numbered Arrays

PHP believes that the first value in an array is numbered zero (0). Of course, humans tend to believe that lists start with the number one (1). This fundamentally different way of viewing lists results in us humans believing an array isn’t working correctly when it is indeed working just fine. For example, consider the following statements:

$test = 1;
while ( $test <= 3 )
{
   $array[] = $test;
   $test++;
}
echo $array[3];

No output (or an error notice) results. I leap to the conclusion that there is something wrong with my loop. Actually, it’s fine. It just results in the following array:

$array[0]=1
$array[1]=2
$array[2]=3

and doesn’t set anything into $array[3].

Including PHP Statements

When a file is read in by using an include statement in a PHP section, it seems reasonable to me that the statements in the file will be treated as PHP statements.

After all, PHP adds the statements to the script at the point where I include them. However, PHP doesn’t see it my way. If a file named file1.inc contains the following statements:

if ( $test == 1 )
echo “Hi”;

and I read it in with the following statements in my main script:

<?php
$test = 1;
include (“file1.inc”);
?>

I expect the word Hi to display on the Web page. However, the Web page actually displays this:

if ( $test == 1 ) echo “Hi”;

Clearly, the file that is included is seen as HTML. To send Hi to the Web page, file1.inc needs to contain the PHP tags.

<?php
if ( $test == 1 )
echo “Hi”;
?>

Missing Mates

Parentheses and curly brackets must come in pairs. Opening with a ( that has no closing ) or a { without a } results in an error message. One of my favorite examples of this is when I use one closing parenthesis where two are needed, as in the following statement:

if ( isset($test)

This statement needs a closing parenthesis at the end. It’s much more difficult to spot that one of your blocks didn’t get closed when you have blocks inside of blocks inside of blocks. For example, consider the following:

while ( $test < 3 )
{
    if ( $test2 != “yes” )
    {
       if ( $test3 > 4 )
       {
          echo “go”;
       }
}

You can see there are three opening curly brackets but only two closing ones. Imagine that 100 lines of code are inside these blocks. It can be difficult to spot the problem — especially if you think the last closing bracket is closing the while loop, but PHP sees it as closing the if loop for $test2. Somewhere later in your script, PHP may be using a closing bracket to close the while loop that you aren’t even looking at. It can be difficult to trace the problem in a large script.

Indenting blocks makes it easier to see where closing brackets belong. Also, I often use comments to keep track of where I am, such as

while ( $test < 3 )
{
    if ( $test2 != “yes” )
    {
          if ( $test3 > 4 )
          {
              echo “go”;
          } # closing if block for $test3
    } # closing if block for $test2
} # closing while block

Confusing Parentheses and Brackets

I’m not sure whether this is a problem for everyone or just a problem for me because I refuse to admit that I can’t see as well as I used to. Although PHP has no trouble distinguishing between parentheses and curly brackets, my eyes are not so reliable. Especially while staring at a computer screen at the end of a 10-hour programming marathon, I can easily confuse ( and {. Using the wrong one gets you a parse error message.


PHP

Leave a Reply