What is a bug?

While attending O'Reilly's Open Source Convention 2004, I met a chap called Donald Bales. He's a published author (if you're into Oracle and JDBC, there are no better books than his) who also works full-time as a enterprise application development consultant. What he said to me was that he doesn't call a bug a "bug"; it's a "defect". The very best consultants are invariably a good mix between a talented programmer and a skilled dipomat, so I can see where "defect" applies. For the rest of us, though, I think it's important to use the word "bug" because most programmers immediately understand what it means, whereas "defect", while better for management types, is a little hazy.

So, what is a bug? There are several types:

  • A syntax error is where you've managed to physically type your script incorrectly: you're missing a semi-colon, a quote, or you've got too many braces.

  • A semantic error is where your code is correct, but probably not what you intended: adding 5 to "foo" is acceptable PHP, but usually wrong.

  • A logic error is where your code takes the wrong path because you got confused, for example using && rather than || or not using a ! to negate an if statement.

  • A runtime error is where PHP has loaded your code, parsed it, and everything is fine, but it cannot be executed. Perhaps you've called a function from an extension that isn't loaded, perhaps you've supplied the wrong password for an FTP server, etc.

I know this is a huge, huge generalisation, but I venture to say that you'll find most of your errors are of the first type: syntax errors. Fortunately they are the easiest to find and fix, and aren't fatal: PHP always catches syntax errors before it attempts to execute the script, so you know that as soon as no more syntax errors are caught there won't ever be any as long as you don't change it.

On the flip side, real logic errors are a little harder to spot, and may lurk in your code for weeks (years?) before they come to light. These are a little rarer, though, and it's only practice that can help you solve them.

Over the coming chapters we're going to look at various ways to solve bugs, and, although it's not a requirement to remember what the different types of bugs are, it might help you now and then.

 

Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: The most basic debugging technique >>

Previous chapter: Debugging

Jump to:

 

Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.