The three statements (functions) Eval, Execute, and ExecuteGlobal all execute string expressions, but they are different. Eval evaluates an expression and returns the result.
Syntax: [result = ]eval_r(expression_r)
expression_r is a string of any valid VBScript expression
Example:
Copy the code code as follows:
response.Write(eval_r(3+2)) 'Output 5
3+2 is enclosed in quotes, indicating that it is a string, but in the eyes of Eval, it is executed as an expression 3+2.
Execute executes one or more specified statements. Use colons (:) to separate multiple statements.
Syntax:Execute statements
Example:
Execute response.Write(abc) 'Output abc
Copy code
response.Write(abc) is enclosed in quotes to indicate a string
But in the eyes of Execute, it is executed as a statement response.Write(abc).
ExecuteGlobal executes one or more specified statements in the global namespace.
Syntax:ExecuteGlobal statement
Example:
Copy the code code as follows:
dim c
c = global variable
sub S1()
dim c
c = local variable
Execute response.Write(c) 'Output local variables
ExecuteGlobal response.Write(c) 'Output global variables
end sub
Execute response.Write(c) 'Output global variables
call S1()
The variable c is defined both in the global scope and in the function scope. Execute decides to use local variables or global variables according to its location, while ExecuteGlobal always only recognizes c in the global scope.
Summarize:
Eval only executes one statement. The statement may or may not return a value.
Execute executes one or more statements ignoring the return value of the statement
ExecuteGlobal executes one or more statements and ignores the return value of the statement. When a global variable and a local variable have the same name, the global variable is always used.
Notice:
In VBScript, = is used for assignment and comparison. For example, a=b can be said to assign the value of b to a, or it can be said to determine whether a and b are equal. So, does eval_r(a=b) represent assignment or comparison operation? ?
There is a convention here. In Eval, a=b always means comparison operation, and in Execute and ExecuteGlobal, it always means assignment.