1 / 12

Variables

Variables. INT213 Week 1. What is a Variable. A Named storage area (in RAM) that can hold a value (like a mailbox holding a letter) Contents of a variable can be assigned , changed and viewed within a script Values can consist of several Data Types ex: Numbers or strings (text ). .

jethro
Download Presentation

Variables

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Variables INT213 Week 1

  2. What is a Variable • A Named storage area (in RAM) that can hold a value (like a mailbox holding a letter) • Contents of a variable can be assigned, changed and viewed within a script • Values can consist of several Data Types ex: Numbers or strings (text).

  3. Assigning Values • Structure counter = 10 Data is placed into variables by assigning values. Whatever is on the right side of the equal sign is stored in a variable. Myname = “Fred Flinstone” Value Assignment Operator Variable Name String Value

  4. Variable Rules • Variables must begin with a letter, not an underscore or a number • They cannot have more than 255 characters • They cannot contain a period (full stop) , a space or a dash • You can use an underscore, but not as the first character • Variables are not case sensitive • Cannot be a reserved word (word used by VBScript) The names you use for variables is important. • Describes what the variable holds and is used for • Makes scripts easier to read

  5. Sample Usage Opening Script Tag tells server to process what follows as script <% ‘add apples to oranges Apples = 27 Oranges = 3 TotalFruit = apples + Oranges ‘output the result Response.Write “The are “ & TotalFruit & “ Pieces of fruit” %> Assign Vales to Variables Add Values in Variables together and assign to new Variable Output the total Closing script tag tells the sever the script is complete

  6. Only One Value Please • Variables can contain a variety of data types but can only hold one value at a time. <% myValue = 10 myValue = “Hello There” myValue = 100.45 Response.writemyValue %> As each variable is assigned a value, the previous value is replaced. Output value will be 100.45

  7. Preventing Errors Something as simple as spelling a variable name incorrectly could lead to serious problems <% Wages = 100.00 Raise = .25 NewWage = Wages * Rise Response.write “Your salary increase is “ & NewWage %> OutPut: Your salary increase is 0 This is called a LOGIC error. The script would execute properly, but the result would be incorrect

  8. Preventing Errors • Option Explicit and DIM <% Option Explicit Dim Wages, Raise, NewWage Wages = 100.00 Raise = .25 NewWage = Wages * Raise Response.write “Your salary increase is “ & NewWage %> If any variable after the DIM line is spelled incorrectly, IIS would generate an error. The script would work until the error is corrected. Forces server to test that each variable used is spelled correctly. This command should be on the first line of the script List all variables used in script

  9. Scope of Variables • The Scope of a Variable basically determines what it effects and how long it lives. There are three levels of Variables. • Page level • Session level • Application (global) level

  10. Page Level Variables • At this level, Variables are available to the web page in which they were created. • Moving to another web page erases the values in these variables. • Simple Variables are considered Page Level Name = “Fred” Age = 45 See PageVar_page1.asp for a demo This file is located in the Blackboard week 1folder

  11. Session Variables • Session Variables live within a browser session. Moving from page to page or web site to web site does not erase their values. • Session variables are stored on the server as long as browser is open or until session is timed out • Each time a new browser access the server, a session is created • Can be accessed from any page related to a web site Session(“myage”) = 45 Session Variables cannot be declared The variables will be demonstrated in class See SessionVar_page1.asp for a demo This file is located in the Blackboard week 1folder

  12. Application Variables • These Variables can be accessed by any browser accessing a web site. • Multiple browser session (users) can access and alter these variables • Ex: Sites that tracks and displays number of users currently on-line are using Application Variables Application(“No_of_Users”) = 10 All users entering a web site would see the current value of this variable. See ApplicationVar_page1.asp for a demo This file is located in the Blackboard week 1folder

More Related