PrimitiveType

Common PHP programming errors

We all make errors when writing code. This article, which will grow with time, aims to provide a list of errors which are commonly made when programming in PHP. It will mostly be of interest to novice programmers, but more experienced programmers might enjoy looking back on errors they used to make frequently in their early programming days (and be glad to be past them!)

Assigning a variable a value when you want to test for truth

For example, in the following example the coder intends to test that $i equals 5, but instead assigns the value 5 to $i, and as that expression evaluates to non-zero, the body of the if statement is always executed:

if ($i = 5) // do something

The correct code is:

if ($i == 5) // do something

This error is particularly common when starting out with a C-like programming language, as the double equals sign might not be familiar to you.

Division by zero

Dividing a number by zero doesn't make sense, and PHP complains about it when you try to do it. Therefore, it is often worthwhile checking that the divisor is not zero before performing division operations, rather than simply assuming this will not be the case.

Infinite loop

An infinite loop is often caused by forgetting to increment a counter on each iteration of the loop. The following example illustrates this:

$counter = 0; while ($counter < 10) { print "Looping\n"; }

A corrected version of this code is:

$counter = 0; while ($counter < 10) { print "Looping\n"; $counter++; }

Omitting an opening or closing brace or parenthesis

This is an error that is easy to find when running the script if you make it only once, as there will be an uneven number of brackets and the PHP parser will display an error message. If you make the same mistake more than once, then it depends on other factors as to how easily you'll notice the error, as you might not get an error message. Indenting your code uniformly can help to avoid and spot such errors.

if ($i == 5) { print "i is 5";

is missing the closing curly brace. It should be:

if ($i == 5) { print "i is 5"; }

Redeclaring functions and classes

This is most often caused by including a file with the definition of a function or class more than once within the execution of a script. The simple solution is to use include_once() or require_once() to ensure that such definitions are only encountered once in a script, even if the statement to include them is encountered more often. This will not work if the same function or class names are found in separate include files that are each included within a script.

The problem can also occur when including 3rd party libraries, in which case a function or class name may collide with one of yours, or with another 3rd party library. A good practice is to prefix function and class names with an identifier that is likely to be unique. For example, the Symfony framework often uses "sf" in front of its classes, and as a user of the Symfony framework, you should avoid naming your classes with the same prefix.

Misspelling a variable name

In some languages such as Java, it is harder to make this error, as an unrecognized variable name will cause the compiler to issue an error message. It is still possible to misspell a variable name in Java and have the program compile if the variable is named the same as another variable available within the current scope. In PHP, however, it is very easy to misspell variable names and let such mistakes go unnoticed, since most of the time PHP is not set up to warn you about uninitialized variables, and will register a variable when it first encounters it.

$name = 'John'; echo "$nam\n";// $name misspelt as $nam, but PHP might not warn you, and empty line is printed

Forgetting to close a string

This error is easy to notice as you type if you are using syntax highlighting, as the colour of the string is usually different to the code around it. However, you'll otherwise be notified by an ugly parse error message.

$name = 'John;// forgot to close string $details = $info['details];// again, forgot to close string

Quotes within quotes

Again, if you're using syntax highlighting, it should be possible to notice this error as you type. Quotes of the same type as those used to enclose a string must be escaped with a backslash. So

print "<img src="car.jpg" />";

should be

print "<img src=\"car.jpg\" />";

Attempting to redirect a user after you have sent some of the output to the browser

Redirecting a user requires sending a header to the browser, and all headers must be sent before any of the HTML (or other) output. So if you send some text and then try to redirect a user, PHP will issue a warning. You can control at what point output is sent to the browser using PHP's output buffering functions, ensuring that all print/echo statements, for example, don't take effect until after a certain point in your script. However, in most cases you would be well-advised to structure your statements so that no output is sent until after all headers have been sent.

Attempting to place a cookie on the client's system after you have sent some of the output to the browser

This mistake is very similar to the one above as it also requires sending a header to the browser. The same solutions apply.

Forgetting to place semi-colons at the end of statements

Although PHP doesn't require that you end all statements with a semi-colon, it is advisable that you do to avoid errors where it expects one. For example, the following would generate an error:

print 'An ' print 'error';

At least in the case above you will get an error message, but you could inadvertently create a piece of code which does not work as you intend but which nonetheless does not generate an error message. Such bugs can be very hard to locate and fix.

Declaring a class with an access modifier

In Java and some other languages, you can specify a class to be public, protected, private, etc. But in PHP (at least up to and including PHP 5) you can't do this. A class is always public, and adding an access modifier in the class declaration will cause a parse error.

private class MyClass {}// "private" will generate a parse error class MyClass {}// ok, works and is implicitly public