Making mistakes is a natural part of life, especially if you’re a programmer. Often times you may spend more time fixing bugs than you will spend creating new features for your web application. The more complex of a feature, the more likely a bug will sneak in. Some bugs are practically harmless and no one ever finds them because they’re very subtle and don’t interfere with the application. Other bugs are extremely obvious and will cripple your web site until they are fixed. In this tutorial, you will learn common errors in PHP and how to fix them.
One of the most common PHP errors you’ll receive is a parsing error. It looks like this:
Parse error: parse error in localhost\feature.php on line 24
This happens when PHP doesn’t understand a section of your code. Fortunately, PHP tells you where in the code it got confused. In this example, something probably went wrong around line 24. The error message doesn’t always point directly to the line with the error, but it usually does.
Let’s take a look at the most common errors.
Incorrect Quotes
You have to be consistent with your use of quotes in PHP. For example:
<?php echo "Hello there.'; // starts with a double-quote, ends with a single-quote. ?>
This code could cause us a problem, because PHP is still on the lookout for the ending double quote which wasn’t provided in this code snippet. Even my automatic color coding in the code above is showing a problem, because everything after the quote is blue. Notice in further examples below that this doesn’t happen.
So make sure to keep track of your opening and closing quotes to avoid blowing PHP’s mind.
Missing Dollar Signs
A missing dollar sign in a variable name can be easy to miss. PHP will usually give you a parsing error if you have one, or at the very least your script won’t work.
Incorrect Equal Signs
In PHP, there is a large difference between using one equal sign or two. With one equal sign, you often use it to assign something. For example:
<?php $number = 23; ?>
In this example, we used an equal sign to give $number a value of 23.
However, when we wish to draw a comparison, like in an If Statement, we will use two equal signs. Here’s an example:
<?php if (23 == 23) { echo "23 is in fact equal to 23."; } ?>
So using two equal signs is like asking Is Equal. In this example, we’re asking PHP if 23 is equal to 23. Yes it is, so it will echo out the sentence.
This is a very subtle problem because it won’t always cause PHP to give you an error message. You’ll notice that for some reason your PHP code isn’t working right, even though you’re not receiving an error. If that happens, be sure to double check your equal signs! This may happen to you a lot at first but with time this will be an uncommon mistake. Hopefully!
Misspelled Variable Names
All variables in PHP are case-sensitive. Depending on how your script is setup, PHP may not give an error if you misspell a variable, but it will result in your script not working properly.
Here’s an example of a script that won’t work:
<?php $name = "Mike"; echo $Name; ?>
Notice that $Name is not the same thing as $name, so nothing will be displayed.
This is an easy mistake to make, but it’s also easy to avoid. Many code editors will highlight a variable if you’re trying to use it without giving it a value first. Another way to avoid this is to have a naming convention for your variables so it’s easier to remember how a variable should have been spelled. CamelCase is a popular choice, where you capitalize the beginning of each word in a variable: $FirstName, $HomeOwner, $CuteMonkey, etc.
Missing Semicolons
Every PHP statement must end with a semicolon. If PHP doesn’t see a semicolon, it just keeps reading the code until it does. Check this out:
<?php $firstName = "John"; // we have a semicolon here. $lastName = "Smith" // oops, we're missing a semicolon! echo "My name is $firstName $lastName"; // Should output: My name is John Smith ?>
The missing semicolon on our third line will cause PHP to keep reading into the echo statement and become confused. This would result in an error message like:
Parse error: parse error in localhost\name.php on line 4
Notice that even though the problem (missing semicolon) is in line #3, PHP tells us the error is in line #4. This is because PHP kept reading lines #3 and #4 as one statement.
Missing the Ends
Remember: Parentheses and curly brackets come in twos. If you use an If Statement with its curly bracket {, the end of the If Statement needs to have another bracket } to close it.
To help avoid this problem, indent your code with spaces or tabs. This will help you see where all of your opening and closings belong. For example, which of the following is easier to see?
$number = 123; $name = "Mike"; if ($number == 123) { if ($name == "Mike") { echo "Hi $name!"; } $number + $number = $newNumber; echo $newNumber; } else { echo "Your number is not 123."; }
Or this one?
$number = 123; $name = "Mike"; if ($number == 123) { if ($name == "Mike") { echo "Hi $name!"; } $number + $number = $newNumber; echo $newNumber; } else { echo "Your number is not 123."; }
If you’re like me and most others, then the second set of code lets us easily figure out where our opening and closing brackets belong and relate to each other.
Go back to PHP Debugging and Troubleshooting.