O'Reilly logo

Core PHP Programming, Third Edition by Leon Atkinson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

5.3. Initializing Arrays

In the situations where you want to fill an array with several values before you use it, it can become cumbersome to write an assignment for each element. PHP offers the array function to help in this matter. It takes a list of values and returns an array. Listing 5.4 uses array to build an array of the months of the year.

Each value is just as it would be if it were on the right side of the assignment operator. Commas separate the values. By default, as with using empty brackets, elements are numbered starting with zero. You can override this by using the => operator. In Listing 5.4 I have set January to have the index 1. Each subsequent element is indexed by the next integer.

Listing 5.4. Initializing an array
 <?php ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required