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 Functions Introducing Functions Introducing Functions

Ellen Sun
Ellen Sun
1,605 Points

How come the else statement is echoed?

I am following along in the video and have typed up this code in the workspace:

<?php
$current_user = 'Mike';
function is_mike() {
  global $current_user;
  if($current_user == 'Mike'){
    echo 'It is Mike';
  } else {
    echo 'Nope, it is not Mike';
  }

}

is_mike();

When I click on the 'eye' icon, I am taken to a view in my browser that echos out 'Nope, it is not Mike' when it should echo 'It is Mike'. Why is that?

Did I do something wrong in my code?

edited to add <?php to beginning of code.

3 Answers

Try clearing your cache. People have reported strange cache issues. Also, post a snapshot to your malfunctioning workspace and we can see if we have the same problem. Do that by clicking on the camera icon in the upper right and following the steps to open the snapshot. Then paste the URL here.

Ellen Sun
Ellen Sun
1,605 Points

I think it was the cache issue. I chose to follow along on my computer instead of using the workspace. Thanks for your reply!

Kevin Korte
Kevin Korte
28,148 Points

Weird, I copied and pasted your code exactly and got the correct results. Take a look, here: https://w.trhou.se/p8jblhag3n

Nothing looks wrong with that code.

Ellen Sun
Ellen Sun
1,605 Points

Hey Kevin,

Thanks for your reply. I tried the code offline on my computer and it also worked fine, so I think there was a glitch in using the Treehouse workspace, though I'm not sure what could cause it.

Yeah, in offline it works.