Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP PHP Basics (Retired) PHP Datatypes PHP Datatypes Challenge

echoing arrays

how to echo a value in an array

index.php
<?php

//Place your code below this comment
$integer_one = 1;
$integer_two = 2;
$golden = 1.618;
$bool=true;
$colors= array("red" ,"blue" ,"green");
echo  next($color) . "<br>" ;

?>
Fidel Torres
Fidel Torres
25,286 Points

The answer below are basically correct, but all depends on what do you really want your program to do. 1-The first one helps you to get the 'n' element in the colors array by $colors[0] for position 1. 2-The second one just print all the elements from $colors using the $color iterator. 3- The third one is just a repetition of the first one.

My question is that answer your question, but i guessing you want to do some conditionals with integer_one & integer_two if so comment bellow so I can explain better.

Have a good day.

3 Answers

You could also loop through the array and echo out colors, for example:

<?php
//Place your code below this comment
$integer_one = 1;
$integer_two = 2;
$golden = 1.618;
$bool = true;
$colors = array("red", "blue", "green");

foreach($colors as $color) {
    echo $color;
}
?>

This may be more advanced than what you are covering in PHP Basics at the moment, but I just wanted to present it as an option.

Joseph Perez
Joseph Perez
25,122 Points

You can echo a single color in a array by doing this.

<?php
$colors = array('red', 'blue', 'green');
echo colors[0];

// This would then echo out the word "red".
?>

As Azadi mentioned in his post, indexed arrays in php always start at 0. So to access the first item of your array you would want to start at 0 and then if you wanted the second value then you use 1 etc. Hopefully this helped a little bit. :) If you have anymore questions don't hesitate to ask!

Hello,

Hopefully this will answer your question: echo $color[0] echo $color[1] echo $color[2] ... echo $color[n]

Where the number in the brackets is the entry in which you are wanting to echo. Remember that arrays in many languages, including PHP, start with the first element subscripted as 0. So if you have an array with 10 elements, you can index it using subscripting as above for [0] - [9].