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

Creating Array Elements

Now, in a case like earlier, you already knew what all the variables were going to equal. PHP offers a great way to create your array elements at the same time as creating arrays. This requires the => operator, and the array declaration is done at the time array() is called. Look at the basic structure:

$newarray = array(key => value, key => value, ...)

As you can see, we are calling array() with a list of parameters. You can create as many keys as you want, and all of them will be part of $newarray.

Let’s rewrite the program using this new syntax. This is phpft10-02.php and you can find it on the CD. In addition to changing the preceding program’s syntax, I added <br /> tags to make it cleaner.

<?php $newarray = array(1 ...

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