First published at Monday, 3 December 2007

Warning: This blog post is more then 15 years old – read and use with care.

Evil bugs in your code

From time to time I write some code snippets, which I find really hard to debug, and the code does not do, what it is expected to do. Especially when the code is not tested directly, but only executed in some a bit more complex environment.

Those are 4 typical errors I introduced in my code, and spend some time debugging it, because I found them really hard to spot. Luckily, once I spotted the actual bug, I find it a lot easier the next time the typo occurs. Therefore I want to share those, so that I may save you some minutes of your life hunting stupid bugs.

The missing 'if'

( $test->check( $foo ) ) { // Why is this always executed? }

An additional ;

while ( $line = fread( $fp, 1024 ) ); { $text .= $line; } // Damn, why is $text still empty?

Unexpected operator precendence

// WTH isn't this a valid query? $nodeList = $xpath->query( '//*[@attribute="row_' . $row + 1 . '"]' );

The non working for loop

// Parse error: Unexpected ; in line 23 foreach ( $i = 0; $i < 10; ++$i ) { // Do something }

Know some more?

Do you know more hard to detect trivial code bugs, you may spare me some minutes, if it occurs for me?


s0enke at Monday, 3.12. 2007

if ($blah = 1) { //always true, forgot second = }

sapphirecat at Tuesday, 4.12. 2007

Not exactly common, but I had a big if/elseif/elseif/... chain once. I converted the final 'elseif' to an 'else', and spent half an hour trying to figure out why "else ($var > 1)" was a syntax error. I kept reading it "else $var > 1... that's true... so why is the compiler complaining?"

Mostly, my problems anymore are missing/extra semicolons or parens, which the compiler picks up on before things get too out of hand.

motzel at Tuesday, 4.12. 2007


If you reverse operators it will never be problem again. So instead:

if($blah == 1)

always use

if(1 == $blah)

With such a notation, when you accidentaly forgot one of equal sign, compiler report a syntax error.

John J. at Tuesday, 4.12. 2007


I agree, that's an easy way to prevent that problem. However, you have to be in the habit of writing your code that way. I try to often, but frequently forget because I have been doing it the other way since I was taught it in grade school and all through my schooling.

The other listed common mistakes are easy to prevent too if you follow best practices, but they are also easy to make.

My most common mistakes are missing syntax characters, most often the closing parens for if/else/etc. statements, usually when the case is just a function call (isset(), empty(), etc.). Its easy to catch, but still a delay.

Saur at Wednesday, 5.12. 2007

never had that bugs... it's kinda lame bugs.

"If you reverse operators it will never be problem again" that is nice. thx.

open source at Wednesday, 5.12. 2007

I never did any of this bug ;)

Chris at Thursday, 6.12. 2007

The last minute variable change:

for ($i = 0; $l < 10; ++$i) { // Here we go for the infinite loop }

Patrice at Thursday, 6.12. 2007

You place a ; after the while .:

$i = 0; while ( $i < 10 ); { $i++; //Do Something }

Fatiha at Friday, 7.12. 2007

Put an empty line before:

<? header("file.php"); ?>

and you have that error : Cannot add header information - headers already sent by (output started at [script1]) in [script2] on line [line]


tbela99 at Friday, 7.12. 2007

I just had headaches about this one

//$a is previously null //excpecting $a to be an array if($a = get_some_data($param) && isset($a[1]) { // the following code is never executed


//I should have set $a before testing