我的PHP文件不会计算用户输入,也不会输出段落


My PHP file will not calculate user input and won't output paragraph?

为什么不" $total_pizza_size = $pizza_size;$total_浇头 = $toppings * 1.25;""算?我在这个程序的开头做了我的变量,它不断收到错误"致命错误:/应用程序/XAMPP/xamppfiles 中不支持的操作数类型......PHP 在第 50 行"

当我运行程序时,底部的段落也不会显示。发生了什么事情?

*<!DOCTYPE html>
<html>
<head>
    <title>Your Pizza Order</title>
    <link href="main.css" rel="stylesheet" type="text/css">
</head>
<body>
        <h3>Your Pizza Order</h3><?php
    $pizza_size = array (
        "pizza_size"  => array("medium" => 7, "large" => 9, "family" => 9));
            //size
        $size = filter_input(INPUT_POST,'size');
            echo "You selected size $size @ <br>";
            //toppings
          $toppings = filter_input(INPUT_POST,'toppings',
                    FILTER_SANITIZE_SPECIAL_CHARS,FILTER_REQUIRE_ARRAY);
            if($toppings != NULL) {
                $num_toppings = count($toppings);
                echo "<br>You chose these $num_toppings toppings @ $1.25 each<br>";
                foreach($toppings as $key => $value) {
                    echo "$value<br>";
                }
            }
            //other items
            $other_items = filter_input (INPUT_POST, 'other_items',
                    FILTER_SANITIZE_SPECIAL_CHARS, FILTER_REQUIRE_ARRAY);
            if ($other_items !== NULL) {
                $num_other_items = count($other_items);
                //echo $key. ' = ' . $value . '<br>';
                echo "<br>You also chose extras $num_other_items @ $1.50 each<br>";
                foreach ($other_items as $key2 => $value2) {
                }
            } 
            //request
            $request = filter_input(INPUT_POST,'request');
            $request = nl2br($request,false);
            if($request != '') {
                echo "<br>You made this request:<br>";
                echo $request;
            }
            $total_pizza_size = $pizza_size;
            $total_toppings = $toppings * 1.25;
            $total_other_items = $other_items * 1.50;
            $total = $total_pizza_size + $total_other_items + $total_toppings;
            $total_f ='$'.number_format($total, 2);
            ?>
    <p>Your total amount due on delivery is <?php echo $total_f;  ?></p>
    <p>Your pizza will be delivered
    to:</p><span><?php echo $address_one; ?></span><br>
    <h2>Thanks for ordering from the Pizza Place</h2>
    <p>We will call <span><?php echo $phone_number; ?></span><br></p>

*

您不能对整个数组求和。您必须循环访问数组项...

foreach ($pizza_size as $key => $value) {
 .....
}

似乎$toppings是一个数组,它可能是您错过的地方。也许array_sum($toppings) * 1.25 ?

底部的段落没有显示,因为上面有一个语法错误。脚本将退出。

尝试:

$total_浇头 = 计数($toppings) * 1.25

$total_披萨大小 = $size

不能将标量值 (1.25) X 乘以数组 ($toppings)。$total_pizza_size也有同样的问题。您需要使用选定的大小而不是整个数组。

$pizza_size被定义为数组,而不是整数/实数。

您必须像这样更改代码:

$total_pizza_size = $pizza_size['pizza_size']['medium'];

(编辑:)

$total_pizza_size = $pizza_size['pizza_size'][$size];

如果我理解你的代码。

我不知道传递给$toppings的值类型