|
Common PHP programming errorsWe 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. Assigning a variable a value when you want to test for truthFor example, in the following example the coder intends to test that if ($i = 5)
// do something
The correct code is: if ($i == 5)
// do something
Division by zeroDividing 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. Infinite loopAn 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 parenthesisThis 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 classesThis 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 Misspelling a variable nameIn 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. However, I usually discover these mistakes when running scripts during development, and not later. Forgetting to close a stringThis 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. Quotes within quotesAgain, 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 browserRedirecting 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 browserThis 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 statementsAlthough 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. |
