Christian Little

Information on Everything!

Do It Yourself: Fixing Most PHP Errors Quickly

By Christian Little • Dec 3rd, 2008 • Category: Web Development

Today I’m going to show you how to avoid the most common errors that popup in PHP (and what 80% of help questions end up being about on my webmaster forums). If you can debug the following errors, you’ll prevent about 99% of the errors you’ll ever run into when programming anything. Better still, you’ll reduce the flood of help requests on sites like Digital Point, and you’ll get the problem fixed faster than waiting several hours/days for somebody like me to come in and point out the flaw in your code.

‘ vs “

This is probably the most common problem we see. There is a massive difference between using either of this parsers in your code. Simply put, anything between a set of ” will get parsed, anything put between a set of ‘ will not. What does that mean exactly?

Observe this code:

$foo = 69;
echo “First: $foo “;
echo ‘Second: $foo’;

Now if I actually execute the code, I get the following:

First: 69 Second: $foo

See what happened? The second time I echo’d the $foo variable it just outputted the variable name, not the value.

If you are running into this problem, it won’t even throw an error. PHP thinks you’ve done it on purpose, so it just goes along it’s merry way. But if you are outputting text to the screen, you should be able to see if this is happening and change it.

If you want to save yourself the headache and avoid this type of problem, just never use single quotes when echoing anything to the browser. Problem solved.

Using \”

Ok, so you’re not using single quotes anymore for any echo statements, good job! But what happens if you want to put quotes around something in a string you are outputting? Simple, but escaping the double quote surrounding the text it will appear in the output:

$quote = “I like chicken”;
echo “When asked, the minister replied \”$quote\”.”;

This will output the following:

When asked, the minister replied “I like chicken”.

The most common reason for escaping quotes is if your code is outputting html code to the browser, such as this:

echo “<img src=\”someimage.gif\” />”;

Using .

Sometimes you need to join multiple items together in a single echo statement, as some variables (i.e. system variables) don’t like to be executed inside a “. Things like $_POST, $_GET, $_SESSION, $_SERVER will throw big hissy fits at you if you try to use them in an echo statement surrounded by quotes.

The solution is to join multiple components together in an echo statement using . as a connector:

echo “Your IP address is: ” . $_SERVER['REMOTE_ADDR'] . “. Any questions?”;

When executed, the above code will output this:

Your IP address is: 38.107.191.87. Any questions?

If you try to do that by referencing the $_SERVER within the text and quotes, you’ll get a bunch of errors from PHP and your code will not execute (and chances are the entire script will crash until you do this properly).

The Dreaded T_STRING Error

Alright, so you’ve taken when I’ve shown you so far and everything is working great. You add more code and then test your script, and instead of displaying what you want, you get this really nasty error message instead:

parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING

So you’re sitting there wondering what the hell does that mean?

The error means you’ve screwed up your code syntax and are missing something that needs to be there. Usually this is a missing ; or } or { or ( or )

For example:

echo “I like to eat fish on tuesdays”;
$mycounter = 994;
if($mycounter > 1) {
    echo “Last year I caught $mycounter fish”
} else {
    echo “Last year I didn’t catch any fish”;

If you execute that code, you’ll get the annoying T_STRING error and it will reference Line 4. So you look at line 4 and realize there is a ; missing at the end of the line.

echo “I like to eat fish on tuesdays”;
$mycounter = 994;
if($mycounter > 1) {
    echo “Last year I caught $mycounter fish”;
} else {
    echo “Last year I didn’t catch any fish”;

You execute the code again and it throws the same error, but it references Line 6 this time. Any idea on what you left out?

You forgot to put a } to close the else statement.

echo “I like to eat fish on tuesdays”;
$mycounter = 994;
if($mycounter > 1) {
    echo “Last year I caught $mycounter fish”;
} else {
    echo “Last year I didn’t catch any fish”;
}

Now that code will work and you’ve debugged your first script without having to wait for another coder to look at your code.

Happy Wednesday everybody!


Christian Little is a web monkey and owner of this website. Aside from blogging about webmastering, SEO, and marketing, he spends his time with his family, running too many websites, playing counter-strike, and provides SEO consulting for a few select clients around the world.
Email this author | All posts by Christian Little

3 Responses »

  1. I had a lot of scripts and many of them are obsolete now but that ‘T_String’ error really made me mad because I spent a lot of time fixing that. There was one more error related to “magic quotes”, that is really another damn error. Thanks for sharing a few main tips but I’ll love to know about that error “Magic quotes”. Thanks if you can do any help

    Rate this comment:
    2.5
  2. Having servers with PHP5+ and an application coded in PHP4 can cause serious problems. I could see several sites having such errors when PHP5 was established. I think a good programmer can fix such errors in very little time but we’ll be able to mend such code ourselves.

    Rate this comment:
    2.5
  3. Thank you for such a useful post.It is great.

    Rate this comment:
    2.5

Leave a Reply