Search
Sponsors

Posts Tagged ‘variables’

variables in VBScript

Tuesday, February 26th, 2008

variables in VBScript

Variables

Before you use a variable in VBScript you should declare its name . To do this in VBScript you use the dim keyword like the following examples.

You should follow a few rules when you declare variable names and these are

  1. must begin with a letter
  2. cannot contain a period (.)
  3. cannot have more than 255 characters

dim x
dim y
dim myAge
dim myWage

You could have put these all on the same line like this

dim x , y , myAge , myWage

The initial value of the variable is empty in VBScript

To prevent mistakes when declaring variables you can use Option Explicit before declaring them and this means you must declare all your variables with dim , public or private , if you dont you will get an error . Anyone who has created scripts in JavaScript will see the advantage here where you can declare a variable like this

myvar

but if you make a spelling mistake like myyar later on this will not be flagged and can lead to some strange and hard to find errors.

Types of Variables

In VBScript a variable can hold any a value of any data type and this data type does not have to be set when the variable is declared . This means you can change the data type of your variable in your program .

Datatype Description
Empty Variant is uninitialized . Value is 0 for number variables and a zero length string “” for empty strings
Null Variant which contains no valid data
Boolean either True or False
Byte integer in the range 0 to 255
Integer integer in the range -32768 to 32767
Currency -922,337,203,685,477.5808 to 922,337,203,685,477,5807
Long an integer value in the range -2,147,483,648 to 2,147,483,647
Single single precision floating point number in the range of 3.402823E38 - 45 to 1.401298E - 45 for negative values and 1.401298E - 45 to 3.402823 E 38 for positive values
Double a double precision floating point number
Date/Time a number that represents the date between Jan 1 100 to Dec 31 9999
String a variable length string that can contain 2 billion characters
Object contains an object
Error contains an error number

Variable Tutor part 2

Thursday, February 21st, 2008

Variable Tutor part 2

In part 2 we are going to deal with Data Types and Type Casting amongst other things.

Data Types

These are the data types which uses .

  • string ( text )
  • integer (numeric)
  • double(numeric)
  • array
  • object
  • unknown type

As you can see there are not as many data type are there are in some other languages , this in fact is a help to many a programmer . As a programmer you do not worry about setting these data types , PHP does it for you.

Strings

This is the data type that stores text , this can be single characters , words and even complete sentences . How many strings do you think we have below .

$mywages = “not much”;
$intloan = “3000″;

If you said one you are wrong , if you said two give yourself a pat on the back . remember we enclosed the number with quotation marks so PHP treats this as text information .

Concatenation

This means adding one string to another . If you are coming from another programming language this may (will) catch you out from time to time . In PHP we use the period ( . ) to concatenate strings.For example

$firstname = “Bill”;
$secondname = “Gates”;
echo $firstname . $secondname;

would print on the screen BillGates

Numeric Values

There are 2 different types of numerical data types in PHP , integers and doubles. Integers are basicly whole numbers and doubles are fractional (floating point ) numbers . Here are some examples .

$firstint = 9;
$firstdouble = 23.67;
$secondint = - 76;
$seconddouble = -1239.7432;

The values these types can hold is platform specific but for example the range for an integer is commonly -32768 to 32767 on Windows machines.

Type Casting

As you are aware PHP automatically decides what data type but you do have the option of specifying this yourself . Here is how you can do this .

$luckynumber = 7;
$lucknumber = (string) $luckynumber;

This would give us a string rather than the integer we had previously.

Using gettype and settype functions

These are two functions provided with PHP which help us to determine the data type and set the data type respectively , lets look at examples of both . Firstly settype which will display the data type of the variable

<?php
$myname = “Iain”;
echo gettype($myname);
?>

string
Now for settype , this allows you to set the data type . The first parameter is the actual variable , the second is the data type you require) . In this example we set the data type and then use gettype to show it has changed.

<?php
$wages = 1000;
settype($wages, “string”);
echo gettype($wages);
?>

string

variables in PHP part 1

Thursday, February 21st, 2008

variables in PHP part 1
Variables

A variable is an area of memory which is set aside to store information , this is assigned an identifier by the programmer . You can recognise variables in PHP because they are prefixed with the dollar ( $ ) sign . To assign a variable you use the assignment operator ( = ) . Here is an example of this.

$name = “iain hendry”;
$intDaysInWeek = 7;

In the first example the variable identifier in this example is $name and the string value “iain hendry” has been assigned to it . In the second example the variable identifier is $intDaysInWeek and the number 7 is assigned to it . Note that in the number example we do not surround the variable with quotes in this way PHP treats this as a numeric value but if we had put quotes round it then PHP would have treated it as a string.

Variable Naming

There are some guidelines to follow for naming variables in PHP and these are as follows .
1. All variable names must begin with a letter or underscore character .
2 . The name of the variable can be made up of numbers , letters , underscores but you cant use charcters like + , - , & , £ , * , etc
One important thing to note if you are coming from another programming language there is no size limit for variables .

Case Sensitivity

One thing that causes many hours of hair pulling and anguish is case sensitivity , PHP is case sensitive (some languages are not) . Here is an example of what I mean.

<?php
$myname = “Iain Hendry”;
echo $Myname”;
?>

Now we all know what we want to do , declare a variable , assign it the value of “Iain Hendry” and then print this on the screen but in this example we have mis-spelt the variable name , when run the following error is displayed on the screen.

Warning: Undefined variable: Myname in D:\testsample.php on line 3

Example

Using your favourite HTML editor enter the following

<?php
$url = “http://www.myscripting.com”;
$rank = 1;
echo “Our favourite site is “.$url;
echo “<br>”;
echo “It is number “.$rank;
?>

here is the output you should get

Our favourite site is http://www.myscripting.com
It is number 1

Translate