PHP Anthology, Volume 1, Chapter 1: PHP Basics | 4
PHP Anthology, Volume 1, Chapter 1: PHP Basics
How do I fix an error that PHP finds in my script?
There you are, half way through your latest and greatest script, and all of a sudden a test execution delivers this error:
Parse error: parse error, unexpected T_ECHO, expecting ',' or ';' in c:\htdocs\sitepoint\phpbasics\2.php on line 5
The offending code here is as follows:
Example 1.2. 2.php
<?php echo 'This is some code<br />'; echo 'Somewhere in here I\'ve got a '; echo 'parse error!<br />' echo 'But where is it?<br />'; ?>
What you’re dealing with here is known as a syntax error, and while you’re new to PHP you may find yourself spending a lot of time hunting down such problems. As you get more experienced with PHP, tracking down syntax errors will become easier. You’ll even come to know your own bad habits and probably be able to guess the error you made before you start the hunt (my own typical failings are forgetting the final quote when building SQL statements in a PHP string and leaving out commas when building arrays). Being familiar with PHP’s error messages is a good idea, though.
In general terms, there are four basic types of errors you’ll encounter in your PHP applications:
As in the example above, syntax errors occur when you break the rules of PHP’s syntax. Syntax errors will usually result in a Parse Error message from PHP.
In the example above, the problem itself occurs on line 4:
echo 'parse error!<br />'
I forgot to add at the end of the line the semicolon (;) that’s required to mark the termination of every statement. The PHP parser only noticed the problem on line five when it encountered another echo statement, as instructions may legally span more than one line. This is worth being aware of, as it sometimes makes errors hard to find—an error might actually have occurred prior to the line on which PHP noticed a problem.
Syntax errors can get particularly confusing in the case of large if-else or while statements where, for example, you’ve forgotten a closing parenthesis. Perhaps you have a long listing that’s interspersed by blocks of HTML; finding that missing curly brace may be extremely difficult. However, as your coding technique improves and you start to take advantage of classes, breaking your code up into discrete blocks within which the code is short and easy to read, you’ll find locating syntax errors much easier.
One further thing to be aware of is PHP’s use of tokens . In the above error message, PHP complained about an “unexpected T_ECHO.” A T_ECHO is a token representing an echo statement in your PHP script. The PHP parser breaks your code up into tokens so that it can analyze and process the script. Some of the tokens you’ll see reported in parse errors are less obvious than others, so if you’re unsure, it’s worth looking at the manual on tokens.
If you’re using PHP 4.3.0, you’ll find it includes the so-called tokenizer extension , which allows you to see your script the way the PHP parser views it. For the sake of interest, here’s how you could view the tokenizer’s output:
Semantic errors occur when you write code that obeys the rules of PHP’s syntax, but which, when executed, breaks the “runtime rules” of PHP. For example, the foreach statement expects you to give it an array:
Example 1.4. 4.php
<?php
$variable = 'This is not an array';
foreach ($variable as $key => $value) {
echo $key . ' : ' . $value;
}
?>
Because $variable was not an array, this script produces the following error message:
Warning: Invalid argument supplied for foreach() in c:\htdocs\sitepoint\phpbasics\3.php on line 4
Semantic errors usually result in a Warning error message like this one.
Environment errors occur when a system that’s external to a PHP script causes a problem. For example, your MySQL server might have been down at the point at which your PHP script tried to connect to it. Perhaps you specified an incorrect path to a file you wanted to open, so PHP was unable to find the file.
These errors also occur when we take a PHP script that has been written on one system, and execute it on another system with a different environment. The problem may simply be that the underlying directory structure or domain name of the Web server is different. It’s common to deal with these types of issues by creating a central configuration script that stores all these environment variables.
PHP also has a number of settings in php.ini that can cause a script to fail on another system where the settings are different. I’ll be looking at these in the section called “How do I write portable PHP code?”; there’s also summary information in Appendix A, PHP Configuration .
Logic errors occur when an application runs perfectly as far as the PHP engine is concerned, but the code does something other than what you had intended. For example, imagine you have a mailing script that you want to use to send the same message to a few of the members of your online forum. To your horror, you discover upon executing the script that you’ve mailed the entire forum membership … twenty times!
These kinds of problems are the most difficult to find; users of Windows XP will be well acquainted with Windows updates—even big companies struggle with logic errors.
Critical to finding logic errors is your ability to test rigorously your code in a safe environment that’s separate from your “live” Web server. Thankfully, PHP and related technologies like Apache and MySQL (if you’re using them) are cross platform, which makes putting together an effective development environment easy even if the underlying operating systems are different.
You should also investigate unit testing , a facet of Extreme Programming (XP), to which you’ll find an introduction in Chapter 6, Development Technique . I’ve also suggested further reading at the end of this chapter.
In Chapter 10, Error Handling , I’ll be taking a look at strategies for handling errors themselves, particularly environment errors. In particular, we’ll discuss how you can record (or trap) errors for your analysis without displaying ugly messages to your applications users.
Created: March 27, 2003
Revised: January 2, 2004
URL: http://webreference.com/programming/phpanth1

Find a programming school near you