HTML and CSS Reference
In-Depth Information
As you'll see a bit further on, the ability to specify array indexes using variables is a sta-
ple of some kinds of loops in PHP.
As you've seen, nothing distinguishes between a variable that's an array and a variable
that holds a string or a number. PHP has a built-in function named is_array() that
returns true if its argument is an array and false if the argument is anything else. Here's
an example:
is_array(array(1, 2, 3)); // returns true
is_array('tree'); // returns false
To determine whether a particular index is used in an array, you can use PHP's
array_key_exists() function. This function is often used to do a bit of checking before
referring to a particular index, for example:
if (array_key_exists($state_capitals, “Michigan”)) {
echo $state_capitals[“Michigan”];
As mentioned previously, it's perfectly acceptable to use arrays as the values in an array.
Therefore, the following is a valid array declaration:
$stuff = ('colors' => array('red', 'green', 'blue'),
'numbers' => array('one', 'two', 'three'));
In this case, I have an associative array that has two elements. The values for each of the
elements are arrays themselves. I can access this data structure by stacking the references
to the array indexes, like this:
$colors = $stuff['colors']; // Returns the list of colors.
$color = $stuff['colors'][1]; // Returns 'green'
$number = $stuff['numbers'][0]; // Returns 'one'
The most common data type you'll work with in PHP is the string type. A string is just a
series of characters. An entire web page is a string, as is a single letter. To define a
string, just place the characters in the string within quotation marks. Here are some
examples of strings:
“I like publishing Web pages.”
“This string
spans multiple lines.”
Search WWH ::

Custom Search