Variables

 

Definition

A variable is a symbolic name that stands for a modifiable value. There are several similarities among the variables for the data types.

All variables begin with a dollar sign. Variable names cannot include spaces or special characters. You can use underscores and numbers as long as the variable name does not start with a number. Variable names are case sensitive. In other words, $temp is a different variable name than $Temp.

The following table shows three variable types:
 

Type

Meaning

Examples

int

integer (...-2, -1, 0, 1, 2...)

10, -5, and 0

float

fractional numbers

392.6, 7.0, and -2.667

string

one or more characters

"What's up, chief?"

Any of the above types can be an array. An array is a sequence of a certain data type.

 

Declaring and assigning variables

Declaring a variable indicates that a variable with a certain name is a certain data type. Assigning a variable gives a declared variable a specific value.

The examples below show standard ways of declaring and assigning variables in one step:

int $temp = 3;

float $Temp = 222.222;

string $tEmp = "Heya kid.";

The following examples show declaration and assignment for int, float and string arrays.

int $TEmp[5] = {100, 1000, -70, 2, 9822};

float $TeMp[4] = {43.3, -10.7, 0, 82.5};

string $TemP[3] = {"Lord", "Flies", "cool brown fox2."}; 

If a variable is declared with no assignment, all values are assigned 0, except
for strings which are assigned empty quotation marks.

Strings

A string is a sequence of alphabetical, numerical, or special characters. You can concatenate strings with the + operator.

string $what = "Whale";

string $title = "Great" + " White " + $what;

This creates the title variable with the contents Great White Whale.

Arrays

You can declare an array of int, float or string types. Remember that when accessing an element of the array, the index of the first element is 0. This means that the maximum index of an array is always one less than the number of elements in the array.

Example

string $array[3] = {"first\n", "second\n", "third\n"};

print($array[0]); // Prints "first\n"

print($array[1]); // Prints "second\n"

print($array[2]); // Prints "third\n"

The size of an array increases automatically as needed. Let's say you have an array with two elements. If you try to assign to a third element of the array, the array size automatically increases to three elements. If you query the value of an element beyond the array size, a value of 0 is returned.

int $scores[];    // Declared as a zero element array.

$scores[150] = 3; // Now a 151 element array.

$scores[200] = 5; // Now a 201 element array.

The second statement above gives the array 151 elements and assigns element index 150 the value 3. The third statement expands the array to 201 elements and assigns element index 200 the value 5.

To remove all elements of an array use the clear function. To find the size of an array use the size function.

Example

string $hats[3] = {"blue", "red", "black"};

print("There were " + size($hats) + " hats.\n");

clear($hats);

print("But now there are " + size($hats) + ".\n");

The output from the above statements is:

There were 3 hats.

But now there are 0.

 

 

This website has been archived and is no longer maintained.