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 trialAlexander Walker
327 PointsIf & else Statements, i've changed the value to Cookie Dough and have set the conditionals for my fav, else doesnt work
So i'm struggling with getting the code to execute when i put a conditional of onto the else statement. I'm unsure what to do as i tried to change Randys favourite flavour to a elseif & a if statement but doesnt' work. I tried to $flavor == "cookie dough" in conditional. I also tried overwritting the data $flavor = "cookie dough" while leaving vanilla
<?php
$flavor = "cookie dough";
if ($flavor == "vanilla"){
echo "<p>Your favorite flavor of ice cream is $flavor";
echo ".</p>";}
if($flavor == "cookie dough"){
echo "<p>Randy's favorite flavor is $flavor, also!</p>";
}
else(){}
?>
1 Answer
Perrence Muzavazi
7,633 PointsMake it one condition if block like below, if you use consecutive if statements they are treated as different code blocks block hence the control flow won't execute as expected. . .
<?php
if (condition) {
# code...
}
elseif (condition) {
# code...
}
else {
# code...
}
?>
Alexander Walker
327 PointsAlexander Walker
327 PointsI'm a tard. only needed one if statement and no else. Complete