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<noob />
17,062 Pointswhy 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
231,268 PointsFunctions 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 />
17,062 Pointsi omitted the exercise variables but they exists in the code .
Steven Parker
231,268 PointsBut to understand the issue, it's necessary to see the whole code. A link to a snapshot of your workspace would be ideal.
<noob />
17,062 Pointsthis is the snapshot the file is exercise.php
<noob />
17,062 PointsThanks 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
231,268 PointsI'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 />
17,062 Pointsis there a reason u donβt work with php? treehouse built with php am i right ?
Steven Parker
231,268 PointsThere'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.