VB variable
Variables are "containers" that store information.
![1486187698977981.gif tryitimg.gif](https://img.php.cn/upload/image/214/258/946/1486187698977981.gif)
Try it out - Example (only for IE)
Create and change variables
How to create a variable, assign a value to it, and then Change its value.
Insert variable value in a piece of text
How to insert variable value in a piece of text.
Create an array
Arrays are used to store a series of related data items. This example demonstrates how to create an array to store names.
Remember the algebra you learned in school?
Remember algebra you learned in school? x=5, y=6, z=x+y
Do you remember? A letter (such as x) can hold a value (such as 5), and using the information above, the value of z can be calculated to be 11.
These letters are called variables. Variables can be used to hold values (x=5) or expressions (z=x+y).
VBScript Variables
In contrast to algebra, VBScript variables are used to hold values or expressions.
A variable can have a short name, such as x, or a more descriptive name, such as carname.
VBScript variable name rules:
must start with a letter
cannot contain a period (.)
cannot exceed 255 characters
In VBScript, all variables are related to the type variant and can store different types of data.
Declare (create) VBScript variables
Creating variables in VBScript usually refers to "declaring" variables.
You can declare VBScript variables through Dim, Public, or Private statements. It looks like this:
Dim carname
Now you have created two variables. The names of the variables are "x" and "carname".
You can also declare a variable in a script by using its name. It looks like this:
Now you have created another variable. The name of the variable is "carname". However, this is not a good practice because you may misspell the variable name in the script, which may cause strange results when the script is run.
If you misspell the variable name, for example, the "carname" variable is misspelled as "carnime", the script will automatically create a new variable named "carnime". To prevent your script from doing this, you can use the Option Explicit statement. If you use this statement, you must declare all variables using dim, public, or private statements.
Place the Option Explicit statement at the top of the script, as follows:
Dim carname
carname=some value
Assign a value to a variable
You can assign a value to a variable as follows:
x=10
The variable name is on the left side of the expression, and the value that needs to be assigned to the variable is on the right side of the expression. Now the variable "carname" has the value "Volvo" and the variable "x" has the value "10".
The lifetime of a variable
The lifetime of a variable refers to how long it can exist.
When you declare a variable in a subroutine, the variable can only be accessed within this program. The variables are also invalidated when the program exits. Such variables are called local variables. You can use local variables with the same name in different subprograms because each variable is recognized only within the program in which it is declared.
If you declare a variable outside a subroutine, all subroutines on your page can access it. The lifetime of such variables begins when they are declared and ends when the page is closed.
VBScript Array Variables
Array variables are used to store multiple values in a single variable.
In the following example, an array containing 3 elements is declared:
The numbers shown in brackets It's 2. Array subscripts start with 0, so the array contains 3 elements. This is an array of fixed capacity. You can assign data to each element of the array like this:
names(1)="Jani"
names(2) ="Stale"
Similarly, you can retrieve the value of any element by using the index number of a specific array element. It looks like this:
You can use up to 60 dimensions in an array. Multidimensional arrays are declared by separating the numbers with commas in parentheses. Here, we declare a 2-dimensional array containing 5 rows and 7 columns:
Assign a value to the two-digit array:
Instance
实例(仅适用于 IE) <html> <body> <script type="text/vbscript"> Dim x(2,2) x(0,0)="Volvo" x(0,1)="BMW" x(0,2)="Ford" x(1,0)="Apple" x(1,1)="Orange" x(1,2)="Banana" x(2,0)="Coke" x(2,1)="Pepsi" x(2,2)="Sprite" for i=0 to 2 document.write("<p>") for j=0 to 2 document.write(x(i,j) & "<br />") next document.write("</p>") next </script> </body> </html>
Run Instance»
Click the "Run Instance" button to view the online instance