Docsity

VBScript and PHP: A Comprehensive Guide to Scripting Languages, Summaries of Computer Applications

Toggle sidebar
bg1
PHP Scripting Language:
Fundamentals and Applications
VBScript and JavaScript
VBScript
VBScript stands for Visual Basic Scripting, which is a subset of Visual Basic
for Applications (VBA). Unlike a compiled programming language, VBScript
is an interpreted language, where the statements of instruction are reduced
to a simple sequence of statements that are then executed by a command
interpreter. The corresponding scripting engine provides this interpreter,
along with other basic services to execute the script.
JavaScript
JavaScript is an interpreted computer programming language. It is a
prototype-based scripting language that is dynamic, weakly typed, and has
first-class functions. JavaScript was formalized in the ECMAScript language
standard and was originally implemented as part of web browsers, allowing
client-side scripts to interact with the user, control the browser,
communicate asynchronously, and alter the document content. However,
JavaScript is also used in applications outside of web pages, such as in PDF
documents, site-specific browsers, and desktop widgets.
Differences between JavaScript and VBScript
Usage: JavaScript is often used for cookies, ads, shopping carts, and
login systems, while VBScript is used in embedded applications and
human-machine interfaces.
Syntax: The syntax for JavaScript is similar to C++, while VBScript
follows the Visual Basic for Applications syntax.
Debugging: VBScript does not have an integrated debugging
environment (IDE).
Functions: JavaScript supports only functions, while VBScript supports
functions and subroutines.
Object-Orientation: Neither JavaScript nor VBScript is truly object-
oriented, and they do not support inheritance.
Case Sensitivity: JavaScript is case-sensitive, while VBScript is not.
Execution Environment: VBScript is both a client and server-side
scripting language, while JavaScript is only a client-side scripting
language.
Features of VBScript
VBScript is a lightweight scripting language with a fast interpreter.
•
•
•
•
•
•
•
•
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c

Partial preview of the text

Download VBScript and PHP: A Comprehensive Guide to Scripting Languages and more Summaries Computer Applications in PDF only on Docsity!

PHP Scripting Language:

Fundamentals and Applications

VBScript and JavaScript

VBScript

VBScript stands for Visual Basic Scripting, which is a subset of Visual Basic for Applications (VBA). Unlike a compiled programming language, VBScript is an interpreted language, where the statements of instruction are reduced to a simple sequence of statements that are then executed by a command interpreter. The corresponding scripting engine provides this interpreter, along with other basic services to execute the script.

JavaScript

JavaScript is an interpreted computer programming language. It is a prototype-based scripting language that is dynamic, weakly typed, and has first-class functions. JavaScript was formalized in the ECMAScript language standard and was originally implemented as part of web browsers, allowing client-side scripts to interact with the user, control the browser, communicate asynchronously, and alter the document content. However, JavaScript is also used in applications outside of web pages, such as in PDF documents, site-specific browsers, and desktop widgets.

Differences between JavaScript and VBScript

Usage : JavaScript is often used for cookies, ads, shopping carts, and login systems, while VBScript is used in embedded applications and human-machine interfaces. Syntax : The syntax for JavaScript is similar to C++, while VBScript follows the Visual Basic for Applications syntax. Debugging : VBScript does not have an integrated debugging environment (IDE). Functions : JavaScript supports only functions, while VBScript supports functions and subroutines. Object-Orientation : Neither JavaScript nor VBScript is truly object- oriented, and they do not support inheritance. Case Sensitivity : JavaScript is case-sensitive, while VBScript is not. Execution Environment : VBScript is both a client and server-side scripting language, while JavaScript is only a client-side scripting language.

Features of VBScript

VBScript is a lightweight scripting language with a fast interpreter.

VBScript is mostly case-insensitive and has a simple syntax, making it easy to learn and implement. VBScript is an object-based scripting language, not an Object-Oriented Programming language. VBScript uses the Component Object Model (COM) to access the elements of the environment in which it is executing. Successful execution of VBScript can only happen in a host environment, such as Internet Explorer (IE), Internet Information Services (IIS), or Windows Scripting Host (WSH).

VBScript Version History and Uses

VBScript was introduced by Microsoft in 1996, and the current stable version is 5.8, available as part of IE8 or Windows 7. VBScript is used in various applications, including:

Automation testing tools like Quick Test Professional (QTP) Windows Scripting Host for automating the Windows Desktop Active Server Pages (ASP) for creating dynamic web pages Client-side scripting in Microsoft Internet Explorer Microsoft Outlook Forms (with VBA for application-level programming)

Language Structure

Your First VBScript

Here's an example of a VBScript that prints "Hello World":

 

The document.write() function is used to write text, HTML, or both into the HTML document.

Whitespace and Line Breaks

VBScript ignores spaces, tabs, and newlines within the script, allowing you to format and indent the code for better readability.

Single Line Syntax

In VBScript, colons are used to write multiple lines of code in a single line, acting as a line separator.

Multiple Line Syntax

When a statement in VBScript is lengthy, you can break it into multiple lines by using the underscore _ character.

Example 2: Two or more declarations are separated by a comma (,). Dim Variable1, Variable

Assigning Values to the Variables

Values are assigned similar to an algebraic expression. The variable name is on the left-hand side, followed by an equal to (=) symbol, and then its value on the right-hand side.

Examples: - Numeric values should be declared without double quotes. B = 200 - String values should be enclosed within double quotes ('). StrValue = "VBScript" - Date and Time variables should be enclosed within hash symbols (#). Date1 = #01/01/2020# Time1 = #12:30:44 PM#

Scalar Variables and Array Variables

A variable containing a single value is a scalar variable. A variable that can contain a series of values is called an array variable. Array variables and scalar variables are declared in the same way, except that the declaration of an array variable uses parentheses () following the variable name.

Example: In the following example, a single-dimension array containing 11 elements is declared: Dim MyArray(10)

Although the number shown in the parentheses is 10, all arrays in VBScript are zero-based, so this array actually contains 11 elements.

You can assign data to each of the elements of the array using an index into the array, starting from 0 and ending at 10. MyArray(0) = 55

Arrays aren't limited to a single dimension. You can have as many as 60 dimensions.

You can declare multiple dimensions by separating an array's size numbers in the parentheses with commas.

Example: In the following example, the MyTable variable is a two- dimensional array consisting of 6 rows and 11 columns: Dim MyTable(5,

The first number is always the number of rows, and the second number is the number of columns.

Dynamic Arrays

You can also declare an array whose size changes during the time your script is running. This is called a dynamic array. The array is initially declared within a procedure using either the Dim statement or the ReDim statement, but no size or number of dimensions is placed inside the parentheses.

Examples: Dim MyArray() ReDim AnotherArray()

To use a dynamic array, you must subsequently use ReDim to determine the number of dimensions and the size of each dimension.

Example: ReDim MyArray(25) ReDim Preserve MyArray(30)

The Preserve keyword is used to preserve the contents of the array as the resizing takes place. There is no limit to the number of times you can resize a dynamic array, although if you make an array smaller, you lose the data in the eliminated elements.

Scope of the Variables

Procedure-level Variables

When you declare a variable within a procedure, only code within that procedure can access or change the value of that variable. It has local scope and is a procedure-level variable.

Script-level Variables

If you declare a variable outside a procedure, you make it recognizable to all the procedures in your script. This is a script-level variable, and it has script-level scope.

Lifetime of Variables

The lifetime of a script-level variable extends from the time it is declared until the time the script is finished running. At the procedure level, a variable exists only as long as you are in the procedure. When the procedure exits, the variable is destroyed.

Variable Declaration Statements

Variables can be declared using the following statements that determine the scope of the variable: Dim Public Private

Dim

Variables declared using the "Dim" keyword at a Procedure level are available only within the same procedure. Variables declared using the "Dim" Keyword at the script level are available to all the procedures within the same script.

Example: ```html

VBScript Fundamentals

Constants

In VBScript, constants are used to store values that do not change during the execution of a program. The following examples demonstrate the use of constants in VBScript:

vbscript Const myString = 'VBScript' Const myDate = #01/01/2050# Msgbox myString Msgbox myDate

In the second example, the user tries to change the constant value, which results in an Execution Error:

vbscript Dim intRadius intRadius = 20 const pi=3.14 pi = pipi 'pi VALUE CANNOT BE CHANGED. THROWS ERROR' Area = piintRadius*intRadius Msgbox Area

Operators in VBScript

Operators in VBScript can be divided into four categories:

Math Operators

Operator | English | Example | Result --- | --- | --- | --- + | Add | 8+7 | 15 - | Subtract | 11-10 | 1 * | Multiply | 7*8 | 56 / | Divide | 8/2 | 4 ^ | Exponent | 2^4 | 16 Mod | Modulus | 15 Mod 10 | 5

Comparison Operators

Operator | English | Example | Result --- | --- | --- | --- = | Equal To | 10 =1 4 | False

| Greater Than | 10 > 14 | False < | Less Than | 10 < 14 | True = | Greater Than Or Equal To | 10 >= 14 | False <= | Less Than Or Equal To | 10 <= 14 | True <> | Not Equal To | 10 <> 14 | True

Logic Operators

Logic operators are used to manipulate and create logical statements. A detailed explanation of Logic and Logic Operators is beyond the scope of this tutorial.

String Concatenation Operator

Operator | English | Example | Result --- | --- | --- | --- & | Connected To | 'Hello' & ' there' | 'Hello there'

Conditional Statements

VBScript provides four conditional statements:

If statement : Executes a set of code when a condition is true. Then...Else statement : Selects one of two sets of lines to execute. If...Then...ElseIf statement : Selects one of many sets of lines to execute. Select Case statement : Selects one of many sets of lines to execute.

If...Then...Else

The If...Then...Else statement is used to execute some code if a condition is true and select one of two blocks of code to execute.

 If i < 10 Then document.write('Good morning!') Else document.write('Have a nice day!') End If ``` **If...Then...ElseIf** The If...Then...ElseIf statement is used to select one of many blocks of code to execute. vbscript i=hour(time) If i = 10 Then document.write('Just started...!') ElseIf i = 16 Then document.write('Time to go home!') End If **Select Case** The Select Case statement is also used to select one of many blocks of code to execute. vbscript d=weekday(date) Select Case d Case 1 document.write('Sleepy Sunday') Case 2 document.write('Monday again!') Case 3 document.write('Just Tuesday!') Case 4 document.write('Wednesday!') Case 5 document.write('Thursday...') Case 6 document.write('Finally Friday!') Case else document.write('Super Saturday!!!!') End Select ## Loop Control Statements VBScript provides the following looping statements: **Do...Loop** : Loops while or until a condition is true. **While...Wend** : Loops while a condition is true. **For...Next** : Uses a counter to run statements a specified number of times. **For Each...Next** : Repeats a group of statements for each item in a collection or each element of an array. **VBScript Functions and Procedures** ## Function Procedures A Function procedure: Is a series of statements, enclosed by the Function and End Function statements Can perform actions and can return a value Can take arguments that are passed to it by a calling procedure Without arguments, must include an empty set of parentheses () Returns a value by assigning a value to its name Example: ```vbscript Function myfunction(a, b) myfunction = a + b End Function document.write(myfunction(5, 9)) ``` The function myfunction will return the sum of argument a and argument b. When you call a procedure, you can use the Call statement, like this: vbscript Call MyProc(argument) Or, you can omit the Call statement, like this: vbscript MyProc argument ## Sub Procedures A Sub procedure: Is a series of statements, enclosed by the Sub and End Sub statements Can perform actions, but does not return a value Can take arguments Example: vbscript Sub mysub() document.write('I was written by a sub procedure') End Sub or vbscript Sub mysub(argument1, argument2) some statements End Sub ## Calling a Function To invoke a function somewhere later in the script, you would simply need to write the name of that function with the Call keyword. Example: ```vbscript 

Function Parameters

Functions can accept parameters, which are passed to the function when it is called. These passed parameters can be captured inside the function and any manipulation can be done over those parameters.

Example: ```vbscript

 ## Returning a Value from a Function A VBScript function can have an optional return statement. This is required if you want to return a value from a function. The values are returned from a function using the function name. In case if you want to return two or more values, then the function name is returned with an array of values. Example: ```vbscript 

Pre-defined Functions in VBScript

VBScript has a number of pre-defined functions, including:

Interaction functions (e.g., InputBox) String manipulation functions Date/time manipulation functions Math functions Data conversion functions Array functions

These pre-defined functions can be used in your VBScript code to perform common tasks and operations.

Advanced Concepts for Functions

VBScript functions can also pass parameters by value (ByVal) or by reference (ByRef). This allows for more advanced control over how parameters are handled within the function.

Additionally, VBScript functions can return multiple values by returning an array of values assigned to the function name.

String Manipulation Functions

Asc, AscB, AsvW Functions

The Asc function returns the ANSI (in the case of Asc) or Unicode (in the case of AscW) character code that represents the first character of the string

Return Value: - Variant of type String.

Notes: - If the string contains a Null, LTrim, RTrim, and Trim return Null.

StrComp Function

The StrComp function returns a value indicating the result of a string comparison.

Arguments: - string1: Required. Any valid string expression. - string2: Required. Any valid string expression. - compare: Optional. Numeric value indicating the kind of comparison to use when evaluating strings.

Return Values: - If StrComp returns: - -1: string1 is less than string2 - 0 : string1 is equal to string2 - 1 : string1 is greater than string2 - Null: string1 or string2 is Null

Example: vbnet Public Function CompStr(ByVal string1, ByVal string2, ByVal compare) Then CompStr = Null Exit Function End If CompStr = Not CBool(StrComp(string1, string2, compare)) End Function

Math Functions

Abs Function

The Abs function returns the absolute value of a number.

Arguments: - number: The number argument can be any valid numeric expression. If the number contains Null, Null is returned; if it is an un- initialized variable, zero is returned.

Return Value: - The absolute value of the argument number.

Cos Function

The Cos function takes an angle specified in radians and returns a ratio representing the length of the side adjacent to the angle divided by the length of the hypotenuse.

Arguments: - number: The number argument can be any valid numeric expression that expresses an angle in radians.

Return Value: - A type of Double denoting the cosine of an angle.

Notes: - If no number is specified, a runtime error is generated. - The cosine returned by the function is between -1 and 1.

Sin Function

The Sin function returns the sine of an angle.

Arguments: - number: The number argument can be any valid numeric expression that expresses an angle in radians.

Return Value: - A Double containing the sine of an angle.

Notes: - If no number is specified, a runtime error is generated. - The ratio is determined by dividing the length of the side opposite the angle by the length of the hypotenuse. The result lies in the range -1 to 1.

Tan Function

The Tan function returns the tangent of an angle.

Arguments: - number: The number argument can be any valid numeric expression that expresses an angle in radians.

Return Value: - A Double containing the tangent of an angle.

Notes: - If no number is specified, a runtime error is generated. - The returned ratio is derived by dividing the length of the side opposite the angle by the length of the side adjacent to the angle.

Log Function

The Log function returns the natural logarithm of a number.

Arguments: - number: The number argument can be any valid numeric expression greater than 0.

Return Value: - A Double containing the Natural Logarithm of a number.

Notes: - If no number is specified, a runtime error is generated. - The natural logarithm is based on e, a constant whose value is approximately 2.718282. The natural logarithm is expressed by the equation: ez = x, where z = Log(x). In other words, the natural logarithm is the inverse of the exponential function. - The number argument, the value whose natural logarithm the function is to return, must be a positive real number. If number is negative or zero, the function generates a runtime error.

Example: vbnet Function Log10(X) Log10 = Log(X) / Log(10) End Function

Conversion Functions

CBool: Converts an expression to Boolean. CByte: Converts an expression to a Byte. CCur: Converts an expression to a Currency. CDate: Converts an expression to a Date.

Parameters:

prompt (required): The message to be displayed in the message box. buttons (optional): A numeric expression that specifies the buttons to display, the icon style, and the message box behavior. title (optional): The text to be displayed in the title bar of the message box.

Data Validation

VBScript provides several functions for data validation, such as IsNumeric, IsDate, and IsArray. These functions can be used to ensure that the user input is of the expected data type.

Integration with Forms

VBScript can be used to interact with HTML forms. The document object provides access to the form elements, and the value property can be used to get or set the values of form fields.

ActiveX Control & Scripting

VBScript can be used to interact with ActiveX controls, which are reusable software components that can be embedded in web pages or other applications. VBScript can be used to control the behavior of these controls and to respond to user events.

Examples

Here are some examples of using VBScript for input, output, and data validation:

 Name: Age: ### ``` **The InputBox Function in VBScript** ## Overview The InputBox function in VBScript is used to display an input dialog box that prompts the user to enter a string value. The function returns the string value entered by the user. Submit ## Parameters The InputBox function accepts the following parameters: **Prompt** : A required parameter that specifies the message to be displayed in the input dialog box. **Title** : An optional parameter that specifies the title of the input dialog box. **Default** : An optional parameter that specifies the default value to be displayed in the input dialog box. **XPos** : An optional parameter that specifies the x-coordinate of the upper-left corner of the input dialog box. **YPos** : An optional parameter that specifies the y-coordinate of the upper-left corner of the input dialog box. **Helpfile** : An optional parameter that specifies the name of the help file to be used for context-sensitive help. **Context** : An optional parameter that specifies the help context number assigned by the help author to the appropriate help topic. ## Examples Here are some examples of using the InputBox function in VBScript: **Input Box with only Prompt** : vbscript InputBox('Enter a number') **Input Box with a Title** : vbscript a = InputBox('Enter a Number', 'Enter Value') msgbox a **Input Box with a Prompt, Title, and Default Value** : vbscript a = InputBox('Enter a Number', 'Enter Value', 123) msgbox a **Input Box with a Prompt, Title, Default, and X-Position** : vbscript a = InputBox('Enter your name', 'Enter Value', 123, 700) msgbox a **Input Box with a Prompt, Title, Default, and Y-Position** : vbscript a = InputBox('Enter your name', 'Enter Value', 123, , 500) msgbox a ## The MsgBox Function The MsgBox function in VBScript is used to display a message box and wait for the user to click a button. The function returns a value that indicates which button the user clicked. The MsgBox function accepts the following parameters: **Prompt** : A required parameter that specifies the message to be displayed in the message box. ## Example ```html 

When the above script is executed, the message box is displayed, and if you press the No Button, then the value of a is 7.

Form Validation

 ## Simple Validation Enter your age: ### ``` ## Data Validation Functions in VBScript **VarType Function** The VarType function returns a value indicating the subtype of a variable. The varname argument can be any variable. The return value is an Integer representing the data type of varname. The VarType Constants can be used to test the return value of the VarType function. **TypeName Function** The TypeName function returns a string containing the name of the data type of a variable. **IsDate Function** The IsDate function returns a Boolean value indicating whether an expression can be converted to a date. The expression can be any date expression or string expression recognizable as a date or time. **IsNumeric Function** The IsNumeric function returns a Boolean value indicating whether an expression can be evaluated as a number. **IsEmpty Function** The IsEmpty function returns a Boolean value indicating whether an expression can be evaluated as a number. Submit **IsNull Function** The IsNull function returns a Boolean value that indicates whether an expression contains no valid data (Null). **IsArray Function** The IsArray function returns a Boolean value indicating whether a variable is an array or contains an array. ## Simple Validation Example The provided HTML code includes a text box and a button. The VBScript code performs simple client-side validation, checking if the entered value is a numeric value between 1 and 10. If the value is valid, a "Thank you" message is displayed; otherwise, an appropriate error message is shown. **Using Numeric Values** The example directly tests the value against a number by using the IsNumeric function to ensure that the string in the text box is a number. Although VBScript automatically converts strings and numbers, it's always a good practice to test a user-entered value for its data subtype and to use conversion functions as necessary. When doing addition with text box values, it's important to convert the values explicitly to numbers because the plus sign (+) operator represents both addition and string concatenation. For example, if Text1 contains '1' and Text2 contains '2', the result would be 'A is 32' instead of 'A is 3'. **Validating and Passing Data Back to the** **Server** The simple validation example uses a plain button control. If a Submit control was used, the example would never see the data to check it— everything would go immediately to the server. Avoiding the Submit control allows the code to check the data, but it doesn't submit the data to the server. To send the data to the server, the code invokes the Submit method on the form object when the data is correct. From there, the server handles the data just as it otherwise would, except that the data is correct before it gets there. ## Form Introduction A form is the central object that manages the other controls. Although the controls can send their data to a script, a form can be used to collect the values typed or selected on the controls, gather them as if they constituted one control, and make these values available to a validating file on a server.