[英]what's wrong with the code
我對php很陌生。 我正在讀一本有關while循環示例的書:
<html>
<body>
<table border="0" cellpadding="3">
<tr>
<td bgcolor="#CCCCCC" align="center">Distance</td>
<td bgcolor="#CCCCCC" align="center">Cost</td>
</tr>
<?
$distance = 50;
while ($distance <= 250) {
echo "<tr>
<td align=\"right\">".$distance."</td>
<td align=\"right\">".($distance / 10)."</td>
</tr>\n";
$distance += 50;
}
?>
</table>
</body>
</html>
當我在Apache Web服務器上運行以下代碼時,結果如下:
\n"; $distance += 50; } ?>
Distance Cost
".$distance." ".($distance / 10)."
我不知道為什么不顯示$distance
的值。 你能幫我解決嗎? 非常感謝你!
用<?php
開始一個代碼塊,而不是<?
。 不要使用短標簽 。
(如果您的書提供了帶有短標簽的PHP示例,以及帶有bgcolor
HTML示例,那么我建議您再購買一個)。
嘗試使用
<?php ?>
而不是
<? ?>
首先,PHP代碼應該以“啟動<?php
”時,請更換“ <?
和“ <?php
”。 然后,該文件應保存為“ .php”文件。
在HTML上下文中(即,您正在編寫html頁面),使用模板樣式會更加清楚:
<html>
<body>
<table border="0" cellpadding="3">
<tr>
<td bgcolor="#CCCCCC" align="center">Distance</td>
<td bgcolor="#CCCCCC" align="center">Cost</td>
</tr>
<?php for($distance = 50; $distance <= 250; $distance += 50): ?>
<tr>
<td align="right"><?php echo $distance ?></td>
<td align="right"><?php echo $distance / 10 ?></td>
</tr>
<?php endfor ?>
</table>
</body>
</html>
這也與所見即所得的Dreamweaver編輯器兼容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.