-1

I'm very new to php. I'm reading a book having an example about while loop:

<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:

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

I don't know why the value of $distance is not printed. Could you help me fix it? Thank you very much!

Sirko
  • 72,589
  • 19
  • 149
  • 183
crazy_in_love
  • 145
  • 3
  • 13
  • off-topic: use CSS rather than inline attributes, unless they're `[class]`es or `[id]`s. – zzzzBov Jul 09 '12 at 14:11
  • PHP doesn't seems to be interpreted. Is `mod_php` enabled? If so, I think @Quentin is right: Do not use short tags! – Florent Jul 09 '12 at 14:11
  • Take a look at the "Related" column on the right of StackOverflow. Please rewrite your question's title, as it is completely meaningless now. – lanzz Jul 09 '12 at 14:12

4 Answers4

5

Start a code block with <?php, not <?. 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).

Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
1

Try using

<?php ?>

rather than

<? ?>
user1507558
  • 444
  • 3
  • 7
  • 15
1

First, the php code should start with "<?php", please replace the "<?" with "<?php". Then, the file should be saved to ".php" file.

0

When in HTML-Context (i.e. you are writing a html-page) it is more clear to use the template-style:

<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.

Ron
  • 1,336
  • 12
  • 20