Skip to content

What do those PHP errors really mean, anyway?

I am teaching an introduction to web programming class with PHP, which means that I spend a lot of time watching students write code. There are really two fundamental skills that the beginning programmer has to master: 1) figuring out the logic that her program should follow, and 2) implementing that logic without errors.

It’s the second type of problem that sends the inexperienced coder running in a panic. Errors are seen as a crushing personal defeat, while code that runs without errors (though not necessarily correctly) is seen as “almost working”, no matter how far from actually functional it is.

I’d like to dispell the myth that errors are a bad thing. Errors are not a bad thing. Errors are a good thing! They tell you exactly what to fix, and exactly where to fix it. The only mystery is in actually interpretting the language of the error, which is another skill that beginning coders have yet to master. What does the PHP interpreter really mean when it says “unexpected T_VARIABLE”? What’s a T_VARIABLE, anyway?

This document is the first in a series that will attempt to help you decode error messages. This version is aimed at very new programmers, working in PHP.

Error 1: unexpected T_VARIABLE

$plant = "bromeliad"
$animal = "pig";
The error means that the PHP interpreter is finding a variable where it does not expect one.

This is almost always due to a missing semi-colon (;) on the line above the line indicated in the error message.

Error 2: Maximum execution time exceeded

$count=10;
while($count>0) {
$count++; }
What fun — you have an infinite loop! That is, a loop that is running continuously without a viable exit condition.

Check all of your loops, especially ‘while’ loops, and make sure that they can plausibly exit. When you construct a loop, you should always consider:

  • The initial condition (is it initialized?)
  • The test condition (does it make sense?)
  • The increment/decrement (does it make sense considering the above?)

If you are writing a ‘counting’ loop (a loop that repeats a known number of times), consider using a for loop. The syntax may be more cryptic, but it’s much harder to inadvertently write an infinite loop!

Error 3: Undefined index

print $_REQUEST['p'];Arrays are essentially variables that hold multiple sets of values (keys and values) under the same name. Each entry in an array has both a key, also called an index, and a value.

This error means that you have attempted to access a particular key that does not exist in your current array. In my experience, this most often occurs with the $_REQUEST array (or $_GET or $_POST), when you are not submitting all form elements to the program.

Make sure that the key should exist in the array at that point in your program. To debug, try printing the array with the print_r command (replace your broken code with: “print_r($_REQUEST);”, where you substitute your array name for $_REQUEST).

Error 4: Syntax error

for(i=0;i>0;i++){}A syntax error is a violation of the grammatical rules of the language. This is a non-specific error. Basically, it means that something in your code is so messed up that the PHP interpreter cannot guess what you might have meant. One common cause of this is forgetting the ‘$’ in front of a variable (as in the example at right). Another possibility is a grammatical issue - check to make sure that all semi-colons, quotes, parenthesis, and brackets are in the right spots.

Related posts

{ 1 } Trackback

  1. [...] 3greeneggs.com: What do those PHP errors really mean, anyway?: I’d like to dispell the myth that errors are a bad thing. Errors are not a bad thing. Errors are a good thing! They tell you exactly what to fix, and exactly where to fix it. The only mystery is in actually interpretting the language of the error, which is another skill that beginning coders have yet to master. What does the PHP interpreter really mean when it says “unexpected T_VARIABLE”? What’s a T_VARIABLE, anyway? [...]