Variables are "containers" for storing information.
Create and change a variableHow
to create a variable, assign a value to it, and then change the value of it.
Insert a variable value in a
textHow to insert a variable value in a text.
Create an arrayArrays are used
to store a series of related data items. This example demonstrates how you can make an array
that stores names.
Do you remember algebra from school? x=5, y=6, z=x+y
Do you remember that a letter (like x) could be used to hold a value (like 5), and that you could
use the information above to calculate the value of z to be 11?
These letters are called variables, and variables can be used to hold values (x=5) or
As with algebra, VBScript variables are used to hold values or expressions.
A variable can have a short name, like x, or a more descriptive name, like Playername.
Rules for VBScript variable names:
In VBScript, all variables are of type variant, that can store different types of data.
Creating variables in VBScript is most often referred to as "declaring" variables.
You can declare VBScript variables with the Dim, Public or the Private statement. Like this:
Now you have created two variables. The name of the variables are "x" and "Playername".
You can also declare variables by using its name in a script. Like this:
Now you have also created a variable. The name of the variable is "Playername".
However, this method is not a good practice, because you can misspell the variable name later in
your script, and that can cause strange results when your script is running.
If you misspell for example the "Playername" variable to "carnime", the
script will automatically create a new variable called "carnime". To prevent
your script from doing this, you can use the Option Explicit statement. This statement forces
you to declare all your variables with the dim, public or private statement.
Put the Option Explicit statement on the top of your script. Like this:
You assign a value to a variable like this:
The variable name is on the left side of the expression and the value you want to assign to the
variable is on the right. Now the variable "Playername" has the value of "Pointing",
and the variable "x" has the value of "10".
How long a variable exists is its lifetime.
When you declare a variable within a procedure, the variable can only be accessed within that
procedure. When the procedure exits, the variable is destroyed. These variables are called local
variables. You can have local variables with the same name in different procedures, because each
is recognized only by the procedure in which it is declared.
If you declare a variable outside a procedure, all the procedures on your page can access it. The
lifetime of these variables starts when they are declared, and ends when the page is closed.
An array variable is used to store multiple values in a single variable.
In the following example, an array containing 3 elements is declared:
The number shown in the parentheses is 2. We start at zero so this array contains 3 elements. A
fixed-size array. You assign data to each of the elements of the array like this:
Similarly, the data can be retrieved from any element using the index of the particular array
element you want. Like this:
You can have up to 60 dimensions in an array. Multiple dimensions are declared by separating the
numbers in the parentheses with commas. Here we have a two-dimensional array consisting of 5
rows and 7 columns:
Asign data to a two-dimensional array:
Your Query was successfully sent!