spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / phpanth1 / 1 To page 1To page 2To page 3current pageTo page 5To page 6
[previous] [next]

Senior Systems Engineer – Disk-Based Backup/Replication (PA)
Next Step Systems
US-PA-Philadelphia

Justtechjobs.com Post A Job | Post A Resume
Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs


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:

Syntax Errors

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:

home / programming / phpanth1 / 1 To page 1To page 2To page 3current pageTo page 5To page 6
[previous] [next]

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint

Created: March 27, 2003
Revised: January 2, 2004

URL: http://webreference.com/programming/phpanth1