所以我从codeacademy获得了这段代码。假设结果是头部,它假设将php,html和css组合在一个while循环中随机输出一个硬币风格的div。然而,它没有工作,所有的PHP都在显示,我不知道为什么。
.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>
&#13;
答案 0 :(得分:0)
问题是关闭&#34; div&#34; ...
附近的while循环中的引号正确的PHP将是:
<?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>";
?>
请注意'coin'
周围的单引号。
答案 1 :(得分: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>
echo '<div class="coin">H</div>';
答案 2 :(得分:0)
使用: -
string