PHP Build a Basic PHP Website (2018) Building a Media Library in PHP Variables and Conditionals

John O mahoney
John O mahoney
578 Points

What is wrong with this code ???

???

index.php
<?php

$flavor = "Chicken";

echo "<p>Your favorite flavor of ice cream is ";
echo "<?php echo $flavor ?>";

if ($flavor == "cookie dough."){
echo "<p>Hal's favorite flavor is cookie dough, also!</p>";
}
else {
echo ".</p>";
}
?>

1 Answer

Joel Bardsley
Joel Bardsley
31,223 Points

Hi John, I've copied your code below and added comments for things that don't look quite right:

<?php

$flavor = "Chicken";

echo "<p>Your favorite flavor of ice cream is ";
echo "<?php echo $flavor ?>"; // No need to use another set of php tags and an echo statement here

if ($flavor == "cookie dough."){ // this requires the user to enter a full stop after cookie dough for this condition to match
echo "<p>Hal's favorite flavor is cookie dough, also!</p>"; // as $flavor equals cookie dough, you can echo the variable instead
}
else {
echo ".</p>"; // no need for an else statement here, if $flavor doesn't equal cookie dough, it can be ignored. But note that if $flavor does equal cookie dough, your code will have two opening <p> tags and one closing </p> tag.
}
?>

If any of the above is unclear, let me know. Good luck.