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

Can someone please tell me why this does not work?

In an attempt to learn basic php, I wrote this program, but it only works if current user is set to "Mickey". Can someone please explain to me my error? Thank you.

<?php

$current_user = "Mickey";

$first_name_arr = array ( 'Mickey', 'Minnie', 'Donald', 'Pluto', 'Friend' );

$last_name_arr = array( 'Mouse', 'Mouse', 'Duck', FALSE );

if($current_user =='Mickey') { $full_name = $first_name_arr[0] . " " . $last_name_arr[0]; echo $full_name; }elseif ($current_user == 'Minnie') { $full_name = $first_name_arr[1] . " " . $last_name_arr[1];

}elseif ($current_user == 'Donald') { $full_name = $first_name_arr[2] . " " . $last_name_arr[2]; }elseif ($current_user == 'Pluto') { $full_name = $first_name_arr[3] . " " . $last_name_arr[3]; }else { $full_name = $first_name_arr[4] . " " . $last_name_arr[4]; }

?>

2 Answers

maybe it is only when the first name is mickey you write it out with echo $full_name;

you can put this line echo $full_name; last so it always will be written out. and you can put $full_name = ""; at the beginning if it won't go into any if-statements.

oh... ok, thank you.. I will try that.

Kevin Korte
Kevin Korte
28,149 Points

Yes, your problem is you only have an echo statement in the Micky block. When any other name gets put in, the full name variable gets generated, but it isn't put to the screen.

Also, just FYI, your final else statement will return an error. You're assigning $last_name to be equal to $last_name_arr[4] which is not in the array, so it errors.