PHP3 Manual | ||
---|---|---|
Prev | Chapter 5. Syntax and grammar | Next |
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.
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"
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();
Prev | Home | Next |
Variable types | Up | Variable Scope |