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 Daily Exercise Program Daily Exercise Program

Yogita Verma
Yogita Verma
5,549 Points

What is problem with my code? Only one condition should be printed but here each condition is being printed.

// store each exercise in a string variable
$exercise1 = 'Display "Hello World!"';
$exercise2 = 'Convert Pounds to Kilograms';
$exercise3 = 'Convert Kilograms to Pounds';
$exercise4 = 'Convert Miles to Kilometers';
$exercise5 = 'Convert Kilometers to Miles';
$exercise6 = 'Month long string of the day';
$exercise7 = 'String of the day with levels';
// create a variable containing the day of the week
$day= date('N');
var_dump($day);
if($day==1)
echo $exercise1;
if($day==2)
echo $exercise2;
if($day==3)
echo $exercise3;
if($day==4)
echo $exercise4;
if($day==5)
echo $exercise5;
if($day==6)
echo $exercise6;
if($day==7)
echo $exercise7;

2 Answers

You are missing your open and closing curly braces around each echo function after the if statements. Should be like so:

if($day1==1)
{
   echo $exercise1;
}

If you notice on your code you have the following:

if($day==1)

echo $exercise1;

Mustafa Başaran
Mustafa Başaran
28,046 Points

In conditional statements, the basic structure is as follows:

if (check condition 1) {
  # some code here
} elseif (check condition2) {
 # some code here
} else {
 #some code here
}

you can check as many conditions as you wish in consecutive elseif statements.

else at the end checks what remains after if and elseif. I hope this is clear.