INT213 – WEEK 1 ASP tags and comments Variables, Constants, and "Literals" Simple Output
ASP files and tags Internet Information Services Microsoft web server Special processing for any URL ending in.ASP ASP files mix objects, VBscript and HTML are ASP tags contain VBscript programming and ASP object commands
Variables What is a Variable? A named storage location is assigned a value The value can change during the run of the script
Variables — Rules Naming Rules begin with a letter, then letters or numbers no punctuation except _ underscore cannot be a VBScript command word Recommendations for naming Variables declare all variables at the top of your file do not use numbers: what is date1 vs. date2? use descriptive terms: counter, name, price capitalize multiple words (camelCase): errorCounter, familyName, unitPriceDiscount
Literals Literals are hard-coded values that do not change. "ABC DEF" is a text or character literal Always enclosed in double quotes 123 is a numeric literal Never enclosed in double quotes Usually used once in a script
Constants are variables that do not change contain literals used more than once ' define constants Const PI = ' pi Const BR = " " ' HTML break Naming rules are same as for Variables Use all capitals for good style and _ to separate words: SCHOOL_NAME = "School of ICT"
Comments ' from single quote to end of line ' area of a circle circleArea = PI * radius * radius How many comments are enough? If your script contained ONLY comments and non-programmer understands it and a programmer could write the code then you have enough comments
Variables NOT Declaring variables - By default, you do not have to declare variables - Logic Errors occur when variables are mystiped e.g. <% count = 1' variable we meant Response.write counter' variable we typed %> - Nothing will be seen on the browser - counter contains the default empty value
Variables Declaring variable names Tell VBScript to spell check variable names. - forces declaration of all variable names - to dimension (declare) a variable name
Variables Declaring and using variable names <% Option Explicit must be 1 st line of VBscript code Dim givenName, familyName givenName = "Scott" familyName = "Mitchell" %> Author familyNane is not a declared variable. ASP server will send error message: Microsoft VBScript runtime (0x800A01F4) Variable is undefined: 'familyNane' /Week1_Variable_demo.asp, line nn
Actual Data Types see Day 3 of textbook re Data Types Integer (numbers), String (text), Boolean (true/false), Currency, Date Understand the difference between data types: Integer 12 is not the same as String "12" sum = ' sum is 3468 department = "12" product = "3456" sku = department + product ' sku is "123456"
Variables Use at the top of.asp files to prevent VBscript variable naming errors. variables have default value of empty meaning default values of "" (zero length string) when system expects text 0 (numeric zero) in most—but not all—numeric contexts. It is best to assign a zero value to variables used as numbers at the top of the script.
Variables – whose are they? ‘SCOPE’ of a Variable How far does it go? How long does it last? Three levels: 1.Page (.asp file) 2.Session 3.Application
Variables – whose are they? Page is everything in an.asp file lasts while the.asp file is processed global variables declared at top of page accessed by all VBScript within the page local variables declared and accessed only within a function or subroutine
Variables – whose are they? Session is a single client using a site's pages within a length of time session variable cannot be declared is an ASP object accessed by VBScript using it: Session("varName") = … accessed by all VBScript in ALL pages in the Application (in the same folder) lasts until client session 'times out' maintained separately for each client by ASP: unique value for each client
Variables – whose are they?
Variables with values