PHP PHP Basics Daily Exercise Program Daily Exercise Program

<noob />
<noob />
16,622 Points

why this is not working?

function getExercise($dayOfWeek) {
    if($dayOfWeek == 1) {
    echo $exercise1;
  } else if($dayOfWeek == 2) {
    echo $exercise2;
  } else if($dayOfWeek == 3) {
    echo $exercise3;
  } else if($dayOfWeek == 4) {
    echo $exercise4;
  } else if($dayOfWeek == 5) {
    echo $exercise5;
  } else if($dayOfWeek == 6) {
    echo $exercise6;
  } else if($dayOfWeek == 7) {
    echo $exercise7;
  } else {
    echo "$dayOfWeek is not a valid day.";
  }

}

$dayOfWeek = date("N");
getExercise($dayOfWeek);

according to the docs i did evreything ok but i still get this error and unable to find the problem:

 Undefined variable: exercise5 in /home/treehouse/workspace/exercise.php on line 58     

5 Answers

Steven Parker
Steven Parker
176,675 Points

Functions in PHP don't automatically have access to global variables. You need to declare them inside the function:

global $exercise1, $exercise2, $exercise3, $exercise4, $exercise5, $exercise6, $exercise7;

OR, you could refer to them this way:

echo $GLOBALS['exercise6'];

<noob />
<noob />
16,622 Points

i omitted the exercise variables but they exists in the code .

Steven Parker
Steven Parker
176,675 Points

But to understand the issue, it's necessary to see the whole code. A link to a snapshot of your workspace would be ideal.

<noob />
<noob />
16,622 Points

http://w.trhou.se/fngegjeows

this is the snapshot the file is exercise.php

<noob />
<noob />
16,622 Points

Thanks steven. I have a question, I'm having a good time so far learning php, did u condiser the track very good? And do u consider php a good backend language and worth learning?

Steven Parker
Steven Parker
176,675 Points

I've only done the beginner PHP track, and it seemed on par with most tracks here. And I don't use PHP myself, but I know it's still used by more websites than any other backend language.

<noob />
<noob />
16,622 Points

is there a reason u don’t work with php? treehouse built with php am i right ?

Steven Parker
Steven Parker
176,675 Points

There's a blog post somewhere detailing the Treehouse back-end development. It's been a while since I saw it, but if I remember correctly it's mostly Ruby/Rails.

For the back-end, I'm almost exclusively a C# developer. But I'm also not a freelancer, so it's not as important for me to keep up with what languages are most popular.