Debugging
  1. Debugging intro
  2. Debugging basics
  3. Debugging errors
  4. Debugging find errors
  5. Debugging best practices
  6. Debugging tools
  7. Debugging summary

Debugging common errors

These are common errors web developers stumble upon in their code.

Typos

An error can be as simple as misspeling something. You can misspell the name of a function, the name of a variable, or anything that prevents your code from working properly.

Javascript code with a few typos:
functionn printText(){ document.wrte("Here is some text") }

Missing quotes

In web development and programming in general a lot of quotes are used. Sometimes you forget to close the quotes when you open them.

Javascript code with missing quotes:
alert("This message will NOT display because of an error!) document.write(This message will not print for the same reason")

Case sensitivity

Many web languages are case sensitive which means that when you declare a variable or function in one case it has to be referenced the same way whenever you use it again. valueOne and ValueOne are two separate variables just as funcOne() and FuncOne() are two separate functions.

Javascript code with case sensitivy errors:
var name = "John" //will generate an error alert("His name is " + Name) function printText{ document.write("Here is some text") } //will generate an error PrintText()

Pointing to the wrong location

When you need to access another file from a webpage like a header file or to open a file to be written to, you need the right path for that file. If you point to the wrong location, you won't be able to accress it.

HTML code pointing to the wrong file location:
<link rel="stylesheet" type="text/css" href="stypes/style.css" />

In the above example we mistyped the name of the directory as stypes instead of styles, now the file won't be called.

Missing parentheses

When you're using parentheses sometimes you will forget the closing parentheses or you could even forget the parentheses completely.

PHP code with missing parentheses:
<?php //missing right parentheses for the function declaration function printText({ echo "This code will not execute because of missing parentheses"; } //completely missing the parentheses for this function declaration function printMoreText{ echo "Parentheses feel upset that you forget about them"; } ?>

Wrong number of brackets

Brackets are used for things like loops, functions, and conditional statements. Using brackets can get confusing because sometimes you have brackets within brackets and you end up using the wrong number of brackets. Example - you have a conditional statement inside a function. You might close the brackets for the conditional statement but forget the close the brackets for the function.

PHP code with wrong number of brackets:
<?php function printMessage(){ $color = "green"; if ($color == "green"){ echo "Great choice."; echo "<br />Greatest color ever!"; } //closing bracket for the function should be here but it is not ?>

Wrong file permissions

When you need to access a file and you can't seem to be able to access it even though you checked that you're pointing to the right location on the server, what's going on? The file permissions on the file are probably what's prevent you from accessing it. You can change file permissions through FTP or directly on the server (if you have direct server access).

PHP code pointing to the a file which doesn't have permissions to be read:
<?php //file will never be opened because it doesn't have read permissions include ("aFile.php"); ?>

Unclosed tags

When you have unclosed tags you might get undesired and strange results like all the text on your page being bold or your page looking really unorganized with the elements not being where they are supposed to be.

HTML code with unclosed tags:
<b>Here is some bold text <p>This paragraph will be bold even though it's not supposed to be</p>

Bad logic

Code doesn't always work the way you plan it to. If there are undesired results, they can usually be traced back to a logic error. These logic errors can occur in your code when you make a mistake in an algorithm or perform an operation you did not intend to.

PHP code containing bad logic:
<?php //will print out 91 //but it should print out 171 echo 9 * 9 + 10; ?>

In the above example we are trying to add 9 + 10 and multiply the result by 9. Instead, 9 is multiplied by 9 and 10 is added to the result.

Change the code to fix the bad logic:
<?php //will print out 171 //What is in the parentheses will be evaluated first echo 9 * (9 + 10); ?>

Incorrect username/password

Occurs when you try to access a resource such as a database with the wrong credentials. Easy to fix as long as you know the correct username and password.

PHP code that will generate incorrect username/password error:
<?php $connect = mysql_connect('localhost', 'John', '12password34'); ?>
© Copyright 2013-2014 Landofcode.com
Terms of use | Privacy policy | Copyright information