程序运行截图如下:
源码如下:
- <?php
- const discount = 0.8;
- $fruit1 = "橙子";
- $fruit2 = "柚子";
- $fruit3 = "橘子";
- $fruit1Num = 1;
- $fruit2Num = 2;
- $fruit3Num = 3;
- $fruit1Price = 6.88;
- $fruit2Price = 3.99;
- $fruit3Price = 3.28;
-
- $fruit1Total = $fruit1Num * $fruit1Price;
- $fruit2Total = $fruit2Num * $fruit2Price;
- $fruit3Total = $fruit3Num * $fruit3Price;
-
- $total = ($fruit1Total * $fruit2Total * $fruit3Total) * discount;
- ?>
-
- <table border="1" cellpadding="1" cellspacing="1">
- <tr>
- <td>商品名称</td>
- <td>购买数量(斤)</td>
- <td>商品价格(元/斤)</td>
- </tr>
- <tr>
- <td><?php echo $fruit1; ?></td>
- <td><?php echo $fruit1Num; ?></td>
- <td><?php echo $fruit1Price; ?></td>
- </tr>
- <tr>
- <td><?php echo $fruit2; ?></td>
- <td><?php echo $fruit2Num; ?></td>
- <td><?php echo $fruit2Price; ?></td>
- </tr>
- <tr>
- <td><?php echo $fruit3; ?></td>
- <td><?php echo $fruit3Num; ?></td>
- <td><?php echo $fruit3Price; ?></td>
- </tr>
- <tr>
- <td colspan="3">
- 商品折扣: <span><?php echo discount; ?></span>
- </td>
- </tr>
- <tr>
- <td colspan="3">
- 打折后商品总价: <?php echo $total; ?>
- </td>
- </tr>
- </table>
这里有几个要注意的地方:
①table中的border是那个表格的外边框,cellpadding指每个小格有多大,cellpadding指每个小格之间的距离;
②td中的colspan是指列的访问,上面的3就是,3个列合为1列。