致命错误:调用未定义的函数money_format()

use*_*312 47 php shopping cart

每次我尝试运行此代码时都会显示一条消息:

Fatal error: Call to undefined function money_format()
Run Code Online (Sandbox Code Playgroud)

有这个问题的线是:

$pricetotal = money_format("%10.2n", $pricetotal);
Run Code Online (Sandbox Code Playgroud)

$cartTotal = money_format("%10.2n", $cartTotal);
Run Code Online (Sandbox Code Playgroud)

你能解释一下这种情况发生的原因吗?

$cartOutput = "";
$cartTotal = "";
$pp_checkout_btn = '';
$product_id_array = '';
if (!isset($_SESSION["cart_array"]) || count($_SESSION["cart_array"]) < 1) {
    $cartOutput = "<h2 align='center'>Your shopping cart is empty</h2>";
} else {
    // Start PayPal Checkout Button
$pp_checkout_btn .= '<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
    <input type="hidden" name="cmd" value="_cart">
    <input type="hidden" name="upload" value="1">
    <input type="hidden" name="business" value="you@youremail.com">';
    // Start the For Each loop
    $i = 0; 
    foreach ($_SESSION["cart_array"] as $each_item) { 
        $item_id = $each_item['item_id'];
        $sql = mysql_query("SELECT * FROM products WHERE id='$item_id' LIMIT 1");
        while ($row = mysql_fetch_array($sql)) {
            $product_name = $row["product_name"];
            $price = $row["price"];
            $details = $row["details"];
        }
        $pricetotal = $price * $each_item['quantity'];
        $cartTotal = $pricetotal + $cartTotal;
        setlocale(LC_MONETARY, "en_US");
        $pricetotal = money_format("%10.2n", $pricetotal);
        // Dynamic Checkout Btn Assembly
        $x = $i + 1;
        $pp_checkout_btn .= '<input type="hidden" name="item_name_' . $x . '" value="' . $product_name . '">
        <input type="hidden" name="amount_' . $x . '" value="' . $price . '">
        <input type="hidden" name="quantity_' . $x . '" value="' . $each_item['quantity'] . '">  ';
        // Create the product array variable
        $product_id_array .= "$item_id-".$each_item['quantity'].","; 
        // Dynamic table row assembly
        $cartOutput .= "<tr>";
        $cartOutput .= '<td><a href="product.php?id=' . $item_id . '">' .     $product_name . '</a><br /><img src="inventory_images/' . $item_id . '.jpg" alt="' . $product_name. '" width="40" height="52" border="1" /></td>';
    $cartOutput .= '<td>' . $details . '</td>';
    $cartOutput .= '<td>$' . $price . '</td>';
    $cartOutput .= '<td><form action="cart.php" method="post">
    <input name="quantity" type="text" value="' . $each_item['quantity'] . '" size="1" maxlength="2" />
    <input name="adjustBtn' . $item_id . '" type="submit" value="change" />
    <input name="item_to_adjust" type="hidden" value="' . $item_id . '" />
    </form></td>';
    //$cartOutput .= '<td>' . $each_item['quantity'] . '</td>';
    $cartOutput .= '<td>' . $pricetotal . '</td>';
    $cartOutput .= '<td><form action="cart.php" method="post"><input name="deleteBtn' . $item_id . '" type="submit" value="X" /><input name="index_to_remove" type="hidden" value="' . $i . '" /></form></td>';
    $cartOutput .= '</tr>';
    $i++; 
} 
setlocale(LC_MONETARY, "en_US");
$cartTotal = money_format("%10.2n", $cartTotal);
$cartTotal = "<div style='font-size:18px; margin-top:12px;' align='right'>Cart Total : ".$cartTotal." USD</div>";
// Finish the Paypal Checkout Btn
$pp_checkout_btn .= '<input type="hidden" name="custom" value="' . $product_id_array . '">
<input type="hidden" name="notify_url" value="https://www.yoursite.com/storescripts/my_ipn.php">
<input type="hidden" name="return" value="https://www.yoursite.com/checkout_complete.php">
<input type="hidden" name="rm" value="2">
<input type="hidden" name="cbt" value="Return to The Store">
<input type="hidden" name="cancel_return" value="https://www.yoursite.com/paypal_cancel.php">
<input type="hidden" name="lc" value="US">
<input type="hidden" name="currency_code" value="USD">
<input type="image" src="http://www.paypal.com/en_US/i/btn/x-click-but01.gif" name="submit" alt="Make payments with PayPal - its fast, free and secure!">
</form>';
}
?>
Run Code Online (Sandbox Code Playgroud)

Jas*_*n W 58

如果您使用的是基于Windows的系统,那么您将无法使用此功能.

仅当系统具有strfmon功能时才定义函数money_format().例如,Windows没有,因此在Windows中未定义money_format().

(Mike W在评论中也指出了这一点)

http://www.php.net/manual/en/function.money-format.php

  • @Floris谢谢.添加 (2认同)
  • 我最终在上面的PHP手册页上以Rafael M. Salvioni提供的polyfill为条件添加了一个'!function_exists('money_format')`。工作正常。令PHP核心团队没有考虑到这一点感到惊讶。 (2认同)

Flo*_*ris 46

有人指出你可能没有这个功能,因为它并不存在于所有操作系统上(参见Mike W的评论或学习者学生的回答).

由于系统中缺少该功能,您可以根据number_format函数编写自己的函数.您似乎希望将数字格式化为美元,因此默认值(decimal = .和千位= ,应该适合您).

function asDollars($value) {
  if ($value<0) return "-".asDollars(-$value);
  return '$' . number_format($value, 2);
}
Run Code Online (Sandbox Code Playgroud)

然后你可以更换

$pricetotal = money_format("%10.2n", $pricetotal);
Run Code Online (Sandbox Code Playgroud)

$pricetotal = asDollars($pricetotal);
Run Code Online (Sandbox Code Playgroud)