O'Reilly logo

PHP for Teens by Maneesh Sethi

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

Defining Arrays with PHP

I’m going to show you a couple cool tricks you can use for defining arrays like this.

Notice that this type of array uses a consecutive list of integers. When using strings or nonconsecutive integers, these tricks won’t work, but when using similar lists these tricks are great.

Skip the Array Key Definition

The first trick is to skip defining the array key. When you begin defining the array with the short syntax and skip the "key" => part (using only a list of values), you get an array with integer indices starting at 0. For example, say you did this:

$testarray = array('a','b','c','d','e');

You get an array with these elements:

$testarray[0]='a'; $testarray[1]='b'; $testarray[2]='c'; $testarray[3]='d'; $testarray[4]='e'; ...

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