When writing your ASP pages, there is one very important step many developers unfortunately forget to do or in some cases are too lazy to do. Always put the effort in and avoid writing sloppy code, use Option Explicit.
Option Explicit requires that all variable names be declared (with the Dim statement). Using Option Explicit to force variable declaration is good for a number of reasons. First, let's look at this code example:
<%
MyName = "michael"
Response.Write(MyNam)
%>
As I have mispelt MyName and written MyNam nothing will be printed out, ASP will create a new variable MyNam and assign an empty string to it. If your code is long then finding this typing mistake can take you hours.Trust me!!
<%
Option Explicit 'Always, always, always!
'declare your variables
Dim MyName
MyNam = "Michael"
Response.Write(MyName)
%>
Note that If you write the code in the style above using Option Explicit you will get an ASP error informing you that MyNam is undefined. You must first use the Dim statement. Debugging becomes alot easier!
Below is a section of code with an example of a spelling mistake. We have declared our variables but unfortunately mispelled one of our variables ie MyNam. Using Option Explicit the undefined variable will be picked up and the error message 'Variable is undefined' will be displayed. This can truely save you an awful lot of time when debugging your code.
Just note that the Option Explicit must come before any HTML code.
<%@ Language="VBScript" %>
<% Option Explicit %>
<html>
<head>
<title>Using Option Explicit</title>
</head>
<body>
<%
'declare your variables
dim MyName, MyAddress, MyfootballTeam
MyNam = "michael"
Response.Write(MyName)
MyAddress = "16 Marki Street"
Response.Write(MyAddress)
MyfootballTeam ="Spurs"
Response.Write(MyfootballTeam)
%>
</body>
</html>
The above code will produce this error message.

To sum up, using Option Explicit forces you to declare all your variables before you use them. If you use a variable you haven't declared then an error will be generated much like the one in the example above.
Read our related tutorial on 'Variables'
Plug and play ASP membership script that integrates with PayPal to let you charge recurring membership fees.
Get your best asp web hosting provider now and save 25%