如何申请number_format();不同的变量?

时间:2014-06-05 07:46:33

标签: php function variables number-formatting

我正在尝试输出每件商品的价格及其各自的数量。但是我也想知道是否会有一个函数可以使用 number_format(); 一次。例如, $ beerQty 只是保存计算价格的变量之一,但我需要将值与number_format函数一起回显,而不在我的HTML中声明它。提前致谢! :)

这是我的PHP代码:

<?php
// CURRENCY
$c          = '$';
// GET ALL INPUTS FROM CUSTOMER
# ChickBurger
$cbS        = $_POST['cbS'];
$cbL        = $_POST['cbL'];
# Fillet Burger
$fbS        = $_POST['fbS'];
$fbL        = $_POST['fbL'];
# Bacon Cheese Burger
$bcR        = $_POST['bcR'];
# French Fries
$ffS        = $_POST['ffS'];
$ffL        = $_POST['ffL'];
# Drinks
$colaQty    = $_POST['colaQty'];
$juiceQty   = $_POST['juiceQty'];
$beerQty    = $_POST['beerQty'];

// PRICES
# ChickBurger
define('CBS',2.75);
define('CBL',3.55);
$cbSQty     = 0.00;
$cbSQty     = $cbS * CBS;
$cbLQty     = 0.00;
$cbLQty     = $cbL * CBL;
# Fillet Burger
define('FBS',2.55);
define('FBL',3.55);
$fbSQty     = 0.00;
$fbSQty     = $fbS * FBS;
$fbLQty     = 0.00;
$fbLQty     = $fbL * FBL;
# Bacon Cheese Burger
define('BCR',4.25);
$bcQty      = 0.00;
$bcQty      = $bcR * BCR;
# French Fries
define('FFS',0.99);
define('FFL',1.99);
$ffSQty     = 0.00;
$ffSQty     = $ffS * FFS;
$ffLQty     = 0.00;
$ffLQty     = $ffL * FFL;
# Drinks
define('COLA',2.75);
define('JUICE',3.55);
define('BEER',4.25);
$cola       = 0.00;
$cola       = $colaQty * COLA;
$juice      = 0.00;
$juice      = $juiceQty * JUICE;
$beer       = 0.00;
$beer       = $beerQty * BEER; 
?>

这是HTML

<table width="600" cellpadding="2" cellspacing="2" border="1">
    <tr align="center">
        <td>Items</td>
        <td>Size</td>
        <td>Quantity</td>
    <tr height="30" align="center">
        <td width="200" rowspan="2">ChickBurger</td>
        <td width="200">Small / $2.75</td>
        <td width="200"><?php echo $c . number_format("$cbSQty",2,".",",") . ' / ' . $cbS; ?></td>
    <tr align="center">
        <td>Large / $3.55</td>
        <td width="200"><?php echo $c . number_format("$cbLQty",2,".",",") . ' / ' . $cbL; ?></td>
    <tr height="30" align="center">
        <td width="200" rowspan="2">Fillet Burger</td>
        <td width="200">Small / $2.25</td>
        <td width="200"><?php echo $c . number_format("$fbSQty",2,".",",") . ' / ' . $fbS; ?></td>
    <tr align="center">
        <td>Large / $3.55</td>
        <td width="200"><?php echo $c . number_format("$fbLQty",2,".",",") . ' / ' . $fbL; ?></td>
    <tr height="30" align="center" rowspan="2">
        <td width="200">Bacon Cheese Burger</td>
        <td width="200">Regular / $4.25</td>
        <td width="200"><?php echo $c . number_format("$bcQty",2,".",",") . ' / ' . $bcR; ?></td>
    <tr height="30" align="center" rowspan="2">
    <td width="200" rowspan="2">French Fries</td>
        <td width="200">Small / $0.99</td>
        <td width="200"><?php echo $c . number_format("$ffSQty",2,".",",") . ' / ' . $ffS; ?></td>
    <tr align="center">
        <td>Large / $1.99</td>
        <td width="200"><?php echo $c . number_format("$ffLQty",2,".",",") . ' / ' . $ffL; ?></td>
        <tr height="30" align="center" rowspan="2">
          <td colspan="3">Drinks</td>
        </tr>
    <tr height="30" align="center" rowspan="2">
        <td width="200">Cola</td>
        <td width="200">$2.75</td>
        <td width="200"><?php echo $c . number_format("$cola",2,".",",") . ' / ' . $colaQty; ?></td>
    <tr height="30" align="center" rowspan="2">
        <td widh="200">Juice</td>
        <td width="200">$3.55</td>
        <td width="200"><?php echo $c . number_format("$juice",2,".",",") . ' / ' . $juiceQty; ?></td>
    <tr height="100" align="center" rowspan="2">
        <td width="200">Beer</td>
        <td width="200">$4.25</td>
        <td width="200"><?php echo $c . number_format("$beer",2,".",",") . ' / ' . $beerQty; ?></td>
    <tr height="30" align="center" rowspan="2">
        <td width="200"></td>
        <td width="200"></td>
        <td width="200"></td>
  </tr>
</table>

1 个答案:

答案 0 :(得分:0)

$prices = array( 'price1' => 0.70, 'price2' => 1.75 );

$price_formarted = array();
foreach($prices as $key => $val) {
    $price_formated[$key] = number_format($val);
}

将您想要的名称放入&#39; price&#39 ;,循环通过foreach,并添加number_format();每个人。

希望这就是你需要/要求的