网络安全 频道

不开TM 用腾讯QQ也能新建TM企业群(图)

操作符 

    如果变量是程序语言的构造块,那么操作符就是使你用变量能构造出有用的程序的粘合剂。你已经看到了一个关于操作符的例子——赋值操作符,它允许你赋值给一个变量。因为PHP相信你已经被宠坏了,所以它也给出了用于算术、字符串、比较和逻辑操作的操作符。 

    熟悉这些操作符的一个好方法就是使用它们在变量上执行算术运算,就像下面例子所示:

<html> <head> </head> <body> <?php // set quantity $quantity = 1000; // set original and current unit price $origPrice = 100; $currPrice = 25; // calculate difference in price $diffPrice = $currPrice - $origPrice; // calculate percentage change in price $diffPricePercent = (($currPrice - $origPrice) * 100)/$origPrice ?> <table border="1" cellpadding="5" cellspacing="0"> <tr> <td>Quantity</td> <td>Cost price</td> <td>Current price</td> <td>Absolute change in price</td> <td>Percent change in price</td> </tr> <tr> <td><?php echo $quantity ?></td> <td><?php echo $origPrice ?></td> <td><?php echo $currPrice ?></td> <td><?php echo $diffPrice ?></td> <td><?php echo $diffPricePercent ?>%</td> </tr> </table> </body> </html>

    上面的程序看起来复杂吗?别害怕,它实际上非常简单。 

    这个脚本的实质在其最上面部分,在这里,我建立了单元价格和数量的变量。接着,我采用PHP的不同数字操作符执行了系列运算,将运算结果存储于不同的变量中。脚本的其他部分涉及将运算结果在表格里面显示出来。 

    如果你愿意,你甚至可以通过一起使用两个操作符在执行运算操作的同时进行赋值操作,下面的两个代码段是等价的:

<?php // this... $a = 5; $a = $a + 10; // ... is the same as this $a = 5; $a += 10; ?>

    如果你不相信,试着将他们都显示出来。

0
相关文章