Lint and Bugs in PHP

27 10 2010

Vector logo of the PHP programming language wi...

Image via Wikipedia

Today I had an interview that was most enjoyable. For me an interview, whatever the outcome, is a good one if it makes me want to blog. In order for that to happen, there has to be an interesting technical discussion. And, today I was engaged in several such discussions.

One of the subjects we discussed was how to detect errors in PHP scripts. There are several ways you can do this. You may embed your code with var_dump() and print_r() or even var_export() statements which while useful can end up becoming litter that you’ll have to clean up sooner or later. You can also use “try and catch” blocks but these will only catch some errors, namely a statement that failed in the “try” section. So, what else can we do?

Well, you could use something that comes with php which is very useful when you run php from the command line. PHP has a built in lint feature, i.e. a syntax checker and you invoke it by typing merely the following at the command line:

php -l name_of_file.php

What the “-l” does is to cause php to check the syntax of your php script and if there are any syntax errors it will report them. Of course there are more errors that can occur than merely syntax errors. In fact, the online manual notes, as follows:

This option won’t find fatal errors (like undefined functions). Use -f if you would like to test for fatal errors too.

A good thing to do when developing is to ensure that PHP notify you about all kinds of errors that may be afflicting your code. You can do this by setting your error reporting to E_ALL in PHP5.3.

While it is nice to have PHP notify you about errors, sometimes the error messages can be difficult to understand or correlate with the code. So, I have become very enamored of the PHP module XDeBug which has a way of colorfully pointing out errors and providing meaningful information that is most helpful. Derick Rethans is its author and you can find out more about this php extension at

To give you a sense of why php -l is of limited usefulness, I’d like to show the results of using it compared to Xdebug . For this purpose, I’m going to present some code that forms a script aptly entitled antiSort.php which makes use of PHP’s shuffle() for presenting a randomized ordering of array values.  See if you can spot an error that I intentionally introduced.

$cards = array(1,2,3,4,5);
echo '<PRE>';
echo 'values  in array(';
foreach ( range(1,5)  as $n) {
	if ($n == 5) {
		echo $n;
	echo $n . ',';
echo ') should no longer be in asc order.
<p>echo '<HR> shuffling the keys ... and this should shuffle the card values with the following result<br>';
$keys = array_keys($cards);
foreach ($keys as  $k) {
     echo $cards[$k] . '<BR>';
<p>echo '<HR><P>shuffling keys of associative array with values 1-5</p>';
$cards .= array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
$keys = array_keys($cards);  // get just keys
shuffle($keys);//  result is randomized numerical indexed array whose values are the keys of $cards
echo '<PRE>';
print_r($keys); // show resulting array of keys
foreach ($keys as $keyval)
       echo 'key is ',$keyval, ' and card value is ',$cards[$keyval], '

Did you catch the error? Well, maybe it’s not a big deal. After all when I ran the syntax checker, it reported that no syntax errors were detected.

I then ran the code with the XDebug extension and received a less than cheery report. The first error message said ” Warning: array_keys() expects parameter 1 to be array, string given in …. on line 25″. Another error message warned that “shuffle() expects parameter 1 to be array, null given in … on line 26”. The last warning informed that “Invalid argument supplied for foreach() in … on line 29”

Imagine if I had merely run php -l and with no syntax errors being detected, I had gone ahead and checked the source into the repository!

Taking a look at the XDebug warnings gives me more than a hint that the code has problems. The very first warning strongly suggests that something is seriously amiss when the parameter for array_keys(), $cards, turns out to be a string instead of an array. Checking the previous line, I note that instead of an assignment occurring, there is concatenation!

But, can you concatenate an array? Not really. You’re suppose to use concatenation as a way of joining strings in PHP. Since concatenation did occur and effect the value of $cards, what might its value be? According to PHP5.3, $cards contains the string ‘ArrayArray’, certainly a weirdly concatenated result .

PHP’s lint checker reported that the code had valid syntax and that was a true statement. The concatenation was certainly syntactically correct; it simply had no logical basis for being in the code!

Having had my share of debugging experiences like the above,  is the reason that I am sold on XDebug and I would encourage you to check it out if you have yet to do so.

This work is licensed under a Creative Commons License




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: