ASP: Variables

Declaring variables in ASP is simple, especially since all variables are of Variant type. What does this mean to you? You don’t have to declare if your variable is an integer, string, or object. You just declare it, and it has the potential to be anything. To declare a variable in ASP/VBScript we use the Dim statement. If you wanted to print out the variables, you could use the ASP ‘Response.Write’ command.

Here is some code creating and assigning values to a couple of variables:

 
<%@ LANGUAGE="VBSCRIPT" %>
<%
'Commented lines starting with an apostrophe
'are not executed in VBScript
'First we will declare a few variables.


Dim myText, myNum

'To declare a variable, you just put the
'word Dim in front of the variable name.
'Let's assign a value to these


myText = "Have a nice day!"
myNum = 5

'Note that myText is a string and myNum is numeric.

'Next we will display these to the user.

Response.Write(myText)

'To concatenate strings in VBScript, use the ampersand
Response.Write(" My favourite number is " & myNum)
%>

The output is: Have a nice day! My favourite number is 5.

In ASP/VBScript, it is possible not to declare variables at all. A variable can appear in the program, though it has never been declared. It is called default declaring. Variable in this case will be of Variant type.

However, such practice leads to errors and should be avoided. For VB to consider any form or module variable that was not declared explicitly as erroneous, Option Explicit statement should appear in the form or module main section before any other statements. Option Explicit demands explicit declaration of all variables in this form or module. If module contains Option Explicit statement, than upon the attempt to use undeclared or incorrectly typed variable name, an error occurs at compile time.

In naming variables in VBScript you must be aware of these rules:

  • Variables must begin with a letter not a number or an underscore
  • They cannot have more than 255 characters
  • They cannot contain a period (.) , a space or a dash
  • They cannot be a predefined identifier (such as dim, variable, if, etc.)
  • Case sensitivity is not important in VBScript

The variable value will be kept in memory for the life span of the current page and will be released from memory when the page has finished executing. To declare variables accessible to more than one ASP file, declare them as session variables or application variables.

admin

admin

Leave a Reply

Your email address will not be published.

Javascript: Variables

A variable’s purpose is to store information so that it can be used later. A variable is a name, or identifier, that represents some data that you set. The name wraps up the data so you can move it around a lot easier, but the name is not the data! A variable’s value can change during the script. You can refer to a variable by name to see its value or to change its value.

JavaScript is an untyped language. This means that JavaScript variables can hold data of any valid type. It takes its type from the data type of what it is holding. You cannot declare a type for variables in JavaScript. There is no facility for saying this variable must be a string, or this one must be a number.

JavaScript also converts types as needed, automatically and behind the scenes.

Declaring the variables

You declare variables in JavaScript with the var keyword. You can declare multiple variables at once. You can also declare a variable and assign it a value at the same time. Until you assign a value to a variable it is undefined.

If you try to declare a variable that already exists, JavaScript will treat it as a simple assignment statement and assign any new value in the declaration statement to the variable. If the duplicate declaration has no assignment, then nothing happens. If you try to assign a value to a non-existent variable, JavaScript will create the variable for you.

Here is some code creating and assigning values to a couple of variables:

 
<script type="text/javascript">
//Commented lines starting with the double
//forward slash will be ignored by JavaScript

//First we will declare a few variables
//and assign values to them


myText = "Have a nice day!";
myNum = 5;
//Note that myText is a string and myNum is numeric.

//Next we will display these to the user.
 
document.write(myText);

//To concatenate strings in JavaScript, use the '+' operator  
document.write("My favourite number is "+ myNum);
</script>

Variable Naming Conventions

  • JavaScript variables must start with a letter or underscore “_”.
  • JavaScipt is case sensitive. 

Case sensitivity is the thing that causes many problems and take hours of finding mistakes. Have a look at the following code:

 
<script type="text/javascript">

var myVar = "WebCheatSheet";
var myvar = "JavaScript Tutorial";

//outputs "WebCheatSheet - JavaScript Tutorial"
document.write(myVar + " - " + myvar);

</script>

Variable Scope and Lifetime

All variables have scope. The scope is the region of the program for which the variable is declared. Variables may be either Global, or Local. Local variables are available only within the section of code in which they were defined. Changes to Local variables are not reflected ‘outside’ their area of definition. When you exit this area, the variable is destroyed. Global variables are available to other JavaScript code. Generally, variables declared as “var” are Global variables. Variables declared inside a function as “var” are local to the function. Variables defined inside a function without the “var” are Global variables. The lifetime of Global variables starts when they are declared, and ends when the page is closed.

 
<script type="text/javascript">

var altitude = 5;                       //GLOBAL

function square( ) {
   base = 17;                            //GLOBAL
   sqr = 0.5*(base + altitude);
   return sqr;
}

function perimeter() {
   var side = 7.5;                       //LOCAL
   prm = 2*side + base;
   return prm;

}
</script>
admin

admin

Leave a Reply

Your email address will not be published.

PHP: Variables

A variable is a holder for a type of data. So, based on its type, a variable can hold numbers, strings, booleans, objects, resources or it can be NULL. In PHP all the variables begin with a dollar sign “$” and the value can be assignes using the “=” operator. The dollar sign is not technically part of the variable name, but it is required as the first character for the PHP parser to recognize the variable as such.

Another important thing in PHP is that all the statements must end with a semicolon “;”. In PHP we needn’t have to specify the variable type, as it takes the data type of the assigned value. The contents of a variable can be changed at any time, and so can its type. To declare a variable, you must include it in your script. You can declare a variable and assign it a value in the same statement.

Here is some code creating and assigning values to a couple of variables:

<?php
//Commented lines starting with the double
//forward slash will be ignored by PHP

//First we will declare a few variables
//and assign values to them


$myText = "Have a nice day!";
$myNum = 5;
//Note that myText is a string and myNum is numeric.

//Next we will display these to the user.

echo $myText;

//To concatenate strings in PHP, use the '.' (period) operator 
echo "My favourite number is ". $myNum
?>

The output is: Have a nice day! My favourite number is 5.

Case Sensitivity

One thing that causes many problems and take hours of finding mistakes is case sensitivity. PHP is case sensitive. Have a look at the following code:

<?php
$myVar = "WebCheatSheet";
$myvar = "PHP tutorial";

echo "$myVar - $myvar"; //outputs "WebCheatSheet - PHP tutorial"
?>

PHP Variable Naming Conventions

There are a few rules that you need to follow when choosing a name for your PHP variables.

  • PHP variables must start with a letter or underscore “_”.
  • PHP variables may only be comprised of alpha-numeric characters and underscores. a-z, A-Z, 0-9, or _ .
  • Variables with more than one word should be separated with underscores: $my_variable.
  • Variables with more than one word can also be distinguished with capitalization: $myVariable.

One important thing to note if you are coming from another programming language there is no size limit for variables.

Variable References

PHP also allows you to do some neat things with variables. It allows you to create aliases for variables, and it also allows you to have variables whose name is a variable. A variable reference, or alias, is a variable assigned to refer to the same information as another variable. To assign an alias to a variable, you use the reference operator, which is an equals sign followed by an ampersand. The following code snippet outputs ‘Have a nice day!’ twice:

<?php
$firstVar = 'nice day!';            //Assign the value 'nice day!' to $firstVar
$secondVar = &$firstVar;            // Reference $firstVar via $secondVar.
$secondVar = "Have a  $secondVar";  // Alter $secondVar...
echo $firstVar;
echo $secondVar; 
?>

Environment Variables

Beyond the variables you declare in your code, PHP has a collection of environment variables, which are system defined variables that are accessible from anywhere inside the PHP code. All of these environment variables are stored by PHP as arrays. Some you can address directly by using the name of the index position as a variable name. Other can only be accessed through their arrays.

Some of the environment variables include:

$_SERVER Contains information about the server and the HTTP connection. Analogous to the old $HTTP_SERVER_VARS array (which is still available, but deprecated).
$_COOKIE Contains any cookie data sent back to the server from the client. Indexed by cookie name. Analogous to the old $HTTP_COOKIE_VARS array (which is still available, but deprecated).
$_GET Contains any information sent to the server as a search string as part of the URL. Analogous to the old $HTTP_GET_VARS array (which is still available, but deprecated).
$_POST Contains any information sent to the server as a POST style posting from a client form. Analogous to the old $HTTP_POST_VARS array (which is still available, but deprecated).
$_FILE Contains information about any uploaded files. Analogous to the old $HTTP_POST_FILES array (which is still available, but deprecated).
$_ENV Contains information about environmental variables on the server. Analogous to the old $HTTP_ENV_VARS array (which is still available, but deprecated).

The code to use the environment variables will be as follows:

<?php
// moderate shortcut
$newVar = $_COOKIE["myFirstCookie"];

// full version
$newVar = $HTTP_COOKIE_VARS["myFirstCookie"];
?>
admin

admin

Leave a Reply

Your email address will not be published.