Troubleshooting a PHP Script - dummies

Troubleshooting a PHP Script

By Janet Valade

You just can’t write scripts without making certain mistakes. The trick is to train yourself to recognize them, roll your eyes, say, “Not again,” and just fix them. One error message that you’ll see many times is

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

This is PHP’s way of saying “Huh?” It means that it doesn’t understand something. This message helpfully points to the file and the line number where PHP got confused. Sometimes it’s directly pointing at the error, but sometimes PHP’s confusion results from an error earlier in the script.

Following are some of the most common errors and how to avoid them.

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. You may be amazed at how long you can stare at something like the following code

$test = 0;
while ( $test = 0 )

and not see why it’s looping endlessly.

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. Voilà! Another parse error. The line must be either

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


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