Variables

May 30, 2017 · View on GitHub

General

A variable is a named area of data storage that has a type and a value. A variable is represented by a VSlot. A variable is created by assigning a value to it. A variable that somehow becomes defined, but is not initialized starts out with the value null.

Variables have names. Distinct variables may have the same name provided they are in different scopes.

A constant is a variable that, once initialized, its value cannot be changed. 

Based on the context in which it is declared, a variable has a scope and a storage duration.

The following kinds of variable may exist in a script:

Kinds of Variables

Local Variables

Syntax

See Semantics below.

Semantics

Except for a function parameter, a local variable is never defined explicitly; instead, it is created when it is first assigned a value. A local variable can be assigned to as a parameter in the parameter list of a function definition or inside any compound statement. It has function scope and automatic storage duration. A local variable is a modifiable lvalue.

Examples

function doit(bool $p1): void {  // assigned the value true when called
  $count = 10;
    ...
  if ($p1)
  {
    $message = "Can't open master file.";
    ...
  }
  ...
}
doit(true);
// -----------------------------------------
function f(): void
{
  $lv = 1;
  echo "\$lv = $lv\n";
  ++$lv;
}
for ($i = 1; $i <= 3; ++$i)
  f();

Unlike the function static equivalent, function f outputs "$lv = 1" each time.

See the recursive function example.

Array Elements

Syntax

Arrays are created via the array-creation operator or the intrinsic array. At the same time, one or more elements may be created for that array. New elements are inserted into an existing array via the simple-assignment operator in conjunction with the subscript operator [].

Semantics

The scope of an array element is the same as the scope of that array's name. An array element has allocated storage duration.

Examples

$colors = ["red", "white", "blue"]; // create array with 3 elements
$colors[] = "green";                // insert a new element

Function Statics

Syntax:

  function-static-declaration:
    static static-declarator-list  ;
  static-declarator-list:
    static-declarator  
    static-declarator-list  ,  static-declarator 
  static-declarator: 
    variable-name  function-static-initializeropt
  function-static-initializer:
    = const-expression

Defined elsewhere

Semantics

A function static may be defined inside any compound statement. It is a modifiable lvalue.

A function static has function scope and static storage duration.

The value of a function static is retained across calls to its parent function. Each time the function containing a function static declaration is called, that execution is dealing with an alias to that static variable. The next time that function is called, a new alias is created.

Examples

function f(): void {
  static $fs = 1;
  echo "\$fs = $fs\n";
  ++$fs;
}
for ($i = 1; $i <= 3; ++$i)
  f();

Unlike the local variable equivalent, function f outputs "$fs = 1", "$fs = 2", and "$fs = 3", as $fs retains its value across calls.

Instance Properties

These are described in class instance properties section. They have class scope and allocated storage duration.

Static Properties

These are described in class static properties section. They have class scope and static storage duration.

Class and Interface Constants

These are described in class constants section and interface constants section. They have class or interface scope and static storage duration.