PHP3 Manual
PrevChapter 5. Syntax and grammarNext

Variable initialization

To initialize a variable in PHP, you simply assign a value to it. For most types, this is straightforward; arrays and objects, however, can use slightly different mechanisms.

Initializing Arrays

An array may be initialized in one of two ways: by the sequential assigning of values, and by using the array() construct (which is documented in the Array functions section).

To sequentially add values to an array, you simply assign to the array variable using an empty subscript. The value will be added as the last element of the array.

$names[] = "Jill";   // $names[0] = "Jill"
$names[] = "Jack";   // $names[1] = "Jack"
      

Initializing objects

To initialize an object, you use the new statement to instantiate the object to a variable.

class foo {
   function do_foo() { 
      echo "Doing foo."; 
   }
}
$bar = new foo;
$bar->do_foo();
      


PrevHomeNext
Variable typesUpVariable Scope