PHP Array Declaration & Traversing

LEVEL: Beginner

Whats in this post?

  1. Code
  2. Output
  3. Background and Explanation of the code

Code


To copy code: Click View Source “View Source” on the right of this code snippet.

Background and Explanation of the code

A PHP array is declared using the array() function. The most common way to traverse an array is by using the foreach statement. Traversing an array is also demonstrated here by using a while loop which uses the sizeof (variable) function.

Output:

1 2 3 4 5 6 7 8 9

a b c d e f g h i