Recommended: How to get started with ASP variables Variables are used to store information. If a variable is declared outside the subroutine, the variable can be changed by any script in the ASP file. If a variable is declared in a subroutine, it will be created and revoked every time the subroutine is executed. Example: Declare the variable as follows: html body % dim name name=Donald Duck response.
In VB scripts, you do not have to define variables or explicitly define their types in other scripting languages. A variable exists when you first use it. However, this feature allows your code to exist widely in typescripts. If you wrongly define a variable name in the code, a new variable will be created. Your script may not work properly, and you may not be aware of this error.
When you use variables, you need to develop the habit of defining them. All you need to do is test the Dim variableName:
%<%Dim IntUserID%>%
IntUserID is now available. For another safety net, use Option Explicit. If you turn on Option Explicit, you will issue an error signal at any time when you use the variable. This sounds boring, but when an error occurs in your script, it can give you some clues, otherwise you will have to find out where the error is.
To use Option Explicit, use the following as the first line of your script:
<% Option Explicit %>
If you want to see what happens when you forget to define a variable, you can run the following code:
<% Option Explicit %>
<:% strName = Request.Form(Name) %>
Because the strName variable (Dim strName) is not defined, you will see the following errors:
Microsoft VBScript runtime error '800a01f4'
Variable is undefined: 'strName'
/e/oe-test.asp, line 10
Using Len
You can use the Len(string) function to determine the length of the text string:
<%
IntString = This is a Simple Sentence.
IntStringLength = Len(IntString)
Response.Write There are & IntStringLength & characters (including spaces) in the sentence show below:
Response.Write & IntString &
%>
If you want to know how Len works manually, you can think of the form you ask the user to enter their five-digit code or three-digit PIN. With Len, you can verify that you have entered enough numbers.
Using Trim
Trimming strings are something you want to get at the beginning. Many times, a string has an extra space at the beginning or end, and if you don't balance it, you may be worried about wasting time on these variables.
<% strName = Request.Form(Name)
strCheckName = Amy Cowen
If strName = strCheckName THEN
Response.Write Success! The names matched.
Else
Response.Write Sorry. The names do not match.
End if
%>
If the value of strName is Amy Cowen, because that is how I input it into the form box and then test if the two variables are the same, the result is not, because Amy Cowen is not Amy Cowen.
Similarly, if you enter Name into the URL:
<% Response.Write & objRec(Name) & >Your Site %>
If any part of the record in Name has extra space, you will quickly execute the error problem.
You can correct a whole string of processes that execute on the left or right:
<% strComments = Request.Form(Comments)
strComments = Trim(strComments)
%>
Assuming the user has entered::
I am having problems installing the software I downloaded.
The above trimming statement will break up the extra space, leaving only the following content:
I am having problems installing the software I downloaded.
Share: ASP programming session skills Anyone who has written a slightly larger ASP knows that Session is really useful. It can be used to record user-owned data variables, which is both safe and convenient. But do you really know how Session works? Perhaps after you understand, you will never dare to use this love-hate object again. Although the method of replacing it is a bit troublesome, but it is still a long time
2 pages in total Previous page 12 Next page