12.11 Array Length

12.11.1 Synopsis

length (array_expression)

12.11.2 Description

This function returns the number of elements stored within array_expression; any valid expression returning an array value can be used:

length ({"foo", 2, "bar"})
length (array [ ])
length (array_1 [ ] + array_2 [ ])

12.11.3 Example

We want to display a set of words separated by vertical lines:

word1 | word2 | word3 | word4

Suppose words are stored in an array;

words [ ] = { "word1", "word2", "word3", "word4" };

We can simply loop over the array (Section 16.2) and display each word; every word but the last will be followed by a vertical line (the | character).

i = 0;
foreach (word in words []) {
  i++;
  echo (word);
  if (i != length (words []))
    echo (" | ");
}

Replacing these words with links you'll get a simple navigation bar. The interesting thing is that if you add a sections to your site, you only need to change the definition of array words [ ]. These lines will work correctly without modifications since there are no magical numbers like array length within them; everything is evaluated from array words [ ].

This manual can be downloaded from http://www.g-cows.org/.