-3

So I got this code from codeacademy. It's supposed combine php, html and css to randomly output a coin-styled div in a while loop as long as the result is heads. However, It's not working, all the php is showing and I have no clue why.

.coin {
    height: 50px;
    width: 50px;
    border-radius: 25px;
    background-color: grey;
 text-align: center;
 font-weight: bold;
 font-family: sans-serif;
 color: white;
 margin: 10px;
 display: inline-block;
 line-height: 50px;
 font-size: 20px;
}
<!DOCTYPE html>
<html>
    <head>
     <link type='text/css' rel='stylesheet' href='style.css'>
  <title>More Coin Flips</title>
 </head>
 <body>
 <p>We will keep flipping a coin as long as the result is heads!</p>
 <?php
 $flipCount = 0;
 do {
  $flip = rand(0,1);
  $flipCount ++;
  if ($flip){
   echo "<div class="coin">H</div>";
  }
  else {
   echo "<div class="coin">T</div>";
  }
 } while ($flip);
 $verb = "were";
 $last = "flips";
 if ($flipCount == 1) {
  $verb = "was";
  $last = "flip";
 }
 echo "<p>There {$verb} {$flipCount} {$last}!</p>";
 ?>
    </body>
</html>
Anjunadeep
  • 91
  • 1
  • 9

3 Answers3

0

The problem is closing a quotation in the while loop near "div"...

The correct PHP would be:

<?php
$flipCount = 0;
do {
    $flip = rand(0,1);
    $flipCount ++;
    if ($flip){
        echo "<div class='coin'>H</div>";
    }
    else {
        echo "<div class='coin'>T</div>";
    }
} while ($flip);
$verb = "were";
$last = "flips";
if ($flipCount == 1) {
    $verb = "was";
    $last = "flip";
}
echo "<p>There {$verb} {$flipCount} {$last}!</p>";
?>

Note the single quote around 'coin'.

Fracsi
  • 2,274
  • 15
  • 24
0
<!DOCTYPE html>
<html>
<head>
    <link type='text/css' rel='stylesheet' href='style.css'>
    <title>More Coin Flips</title>
</head>
<body>
<p>We will keep flipping a coin as long as the result is heads!</p>
<?php
$flipCount = 0;
do {
    $flip = rand(0,1);
    $flipCount ++;
    if ($flip){
        echo '<div class="coin">H</div>';
    }
    else {
        echo '<div class="coin">T</div>';
    }
} while ($flip);
$verb = "were";
$last = "flips";
if ($flipCount == 1) {
    $verb = "was";
    $last = "flip";
}
echo "<p>There {$verb} {$flipCount} {$last}!</p>";
?>
</body>
</html>

qoute mismatch in echo '<div class="coin">H</div>';

Vel
  • 9,027
  • 6
  • 34
  • 66
0

Use this:-

<!DOCTYPE html>
<html>
<head>
    <link type='text/css' rel='stylesheet' href='style.css'>
    <title>More Coin Flips</title>
</head>
<body>
<p>We will keep flipping a coin as long as the result is heads!</p>
<?php
$flipCount = 0;
do {
    $flip = rand(0,1);
    $flipCount ++;
    if ($flip){
        echo "<div class='coin'>H</div>";
    }
    else {
        echo "<div class='coin'>T</div>";
    }
} while ($flip);
$verb = "were";
$last = "flips";
if ($flipCount == 1) {
    $verb = "was";
    $last = "flip";
}
echo "<p>There {".$verb."} {".$flipCount."} {".$last."}!</p>";
?>
</body>

Vel
  • 9,027
  • 6
  • 34
  • 66
raw_orb
  • 183
  • 1
  • 12