简体   繁体   English

代码有什么问题

[英]what's wrong with the code

I'm very new to php. 我对php很陌生。 I'm reading a book having an example about while loop: 我正在读一本有关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>

Here's the result when I run this code on Apache web server: 当我在Apache Web服务器上运行以下代码时,结果如下:

\n"; $distance += 50; } ?>
Distance     Cost
".$distance."   ".($distance / 10)."

I don't know why the value of $distance is not printed. 我不知道为什么不显示$distance的值。 Could you help me fix it? 你能帮我解决吗? Thank you very much! 非常感谢你!

Start a code block with <?php , not <? <?php开始一个代码块,而不是<? . Do not use short tags . 不要使用短标签

(If your book is giving PHP examples with short tags, and HTML examples with bgcolor then I recommend getting a newer one). (如果您的书提供了带有短标签的PHP示例,以及带有bgcolor HTML示例,那么我建议您再购买一个)。

Try using 尝试使用

<?php ?>

rather than 而不是

<? ?>

First, the php code should start with " <?php ", please replace the " <? " with " <?php ". 首先,PHP代码应该以“启动<?php ”时,请更换“ <?和“ <?php ”。 Then, the file should be saved to ".php" file. 然后,该文件应保存为“ .php”文件。

When in HTML-Context (ie you are writing a html-page) it is more clear to use the template-style: 在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>

This is also compatible with wysiwyg-editors like dreamweaver. 这也与所见即所得的Dreamweaver编辑器兼容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM