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

$colors array second value [1] is coming up wrong.

I'm having a problem with the PHP Datatypes Challenge, I tried to do this yesterday.

The question is asking to Echo the Second Value in $colors array. The second value in a 0 based function is 1, so my code is:

echo $colors[1];

The system returns an error saying the code is incorrect. Bummer! It looks like you are echoing the wrong thing, remember that arrays are 0 based

Help?

Thank you!!

6 Answers

Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hi Sherry,

I'm not sure why the compiler is returning an error on your code. I can't speak of the code you didn't post, but the line you have is correct and will return the second value.

I ran through the challenge just now, and that is exactly what I put and it passed. Sometimes a glitch shows up and you need to refresh (restart) the challenge from the beginning to clear the 'mystery error'.

Here is the code up to and including Task 6

<?php

//Place your code below this comment

$integer_one = 1;
$integer_two = 2;
$golden = 1.618;
$bool = true;

$colors = ['red', 'blue', 'green'];

echo $colors[1];

?>

Keep Coding! :)

I'll try to refresh.. I've gone out of the challenge and worked on other tasks, just went back into it and still receiving the same return. I can't finish the tasks until this passes through :(

Thank you so much!

Hi Jason,

Here's the code I put in this time - same as every other :( (it all comes out very well until I get to #6.)

<?php

//Place your code below this comment
$integer_one = 1;
$integer_two = 2;
$golden = 1.618;
$bool = TRUE;
 var_dump ($bool);

$colors = array ();
$colors = array ('red', 'blue', 'green');
 echo $colors [1];

?>
Jason Anders
MOD
Jason Anders
Treehouse Moderator 145,860 Points

Hi Sherry,

Sorry you're still having problems, but I do see a couple problems with your code.

1) The challenge didn't ask for var_dump ($bool). It's not that it's incorrect, but most times the challenges are very picky and will throw a Bummer! if you add what wasn't asked for.

2) Task 4 wants you to add an empty array named $colors. While I used square brackets to create my array, you can do it the way you have (with parenthesis), except you need to remove the space you have between array and the () $array = array();

If you remove that space on both lines, you will pass. Although, the second declaration you have for $colors (where you added the colors asked for) is not needed. You can just add the colors into the first one you declared. $colors = array('red', 'blue', 'green');.

I hope this helps. :)

Thank you so much, I'll work through the process again. Also "var_dump ($bool) was added because simply coding is as $bool = TRUE; did not run through the code so I researched it and found that the var_dump worked.

Thanks for the heads up on spacing ;) I also tried the $colors = array('red', 'blue', 'green'); on the same line as the previous statement and the system didn't like it.... Oh boy.. fun stuff here. Thanks again for your help.

V/r Sherry

HA! It passed through this time :) I also took out the var_dump and tightened up the spaces :) Thank You Jason!