home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


Book HomeWebmaster in a Nutshell, 3rd EditionSearch this book

16.5. Data Types

PHP provides four primitive data types: integers, floating point numbers, strings, and booleans. In addition, there are two compound data types: arrays and objects.

16.5.5. Arrays

An array is a compound data type that can contain multiple data values, indexed either numerically or with strings. For example, an array of strings can be written like this:

$var[0]="Hello";
$var[1]="World";

Note that when you assign array elements like this, you do not have to use consecutive numbers to index the elements.

As a shortcut, PHP allows you to add an element onto the end of an array without specifying an index. For example:

$var[ ] ="Test";

PHP picks the next logical numerical index. In this case, the "Test" element is given the index 2 in our $var array: if the array has nonconsecutive elements, PHP selects the index value that is one greater than the current highest index value. This autoindexing feature is most useful when dealing with multiple-choice HTML <select> form elements, as we'll see in a later example.

Although we have called strings a primitive data type, it is actually possible to treat a string as a compound data type, where each character in the string can be accessed separately. In other words, you can think of a string as an array of characters, where the first character is at index 0. Thus, you can pick the third character out of a string with:

$string[2]

To solve an ambiguity problem between strings and arrays, a new syntax has been introduced to dereference individual characters from strings:

$string{2}

This syntax is equivalent to $string[2], and is preferable.

Arrays can also be indexed using strings; these kinds of arrays are called associative arrays:

$var["January"]=1;
$var["February"]=2;

You can use a mix of numerical and string indices with a single array because PHP treats all arrays as hash tables internally, and the hash, or index, can be whatever you want.

All arrays in PHP can be traversed safely with the following mechanism:

foreach($array as $key=>$value) {
  echo "array[$key]=$value<br>\n";
}

This is the most common way to loop through each element of an array, whether it is a linear or an associative array. PHP provides a number of array manipulation functions; these are detailed later in the "Function Reference."

16.5.6. Objects

An object is a compound data type that can contain any number of variables and functions. PHP's support for objects is somewhat limited in Version 4. PHP Version 5 will improve the object-oriented capabilities of PHP. In PHP 4, the object-oriented support is designed to make it easy to encapsulate data structures and functions in order to package them into reusable classes. Here's a simple example:

class test {
  var $str = "Hello World";
  function init($str) {
    $this->str = $str;
  }
}

$class = new test;
echo $class->str;
$class->init("Hello");
echo $class->str;

This code creates a test object using the new operator. Then it sets a variable called str within the object. In object-speak, a variable in an object is known as a property of that object. The test object also defines a function, known as a method, called init(). This method uses the special-purpose $this variable to change the value of the str property within that object.

Inheritance is supported by using the extends keyword in the class definition. We can extend the previous test class like this:

class more extends test {
  function more( ) {
    echo "Constructor called";
  }
}

This means that the more class inherits from the test class and it also introduces the concept of a constructor. If a method inside a class has the same name as the class, it becomes the constructor function for that class. A constructor is called automatically when the class is instantiated.

Much more information is available at http://www.php.net/oop.



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.