警告:number_format()要求参数1为float,第205行D:\xampp\htdocs\website\foodpoint\welcome.php中给出的字符串

警告:number_format()要求参数1为float,第205行D:\xampp\htdocs\website\foodpoint\welcome.php中给出的字符串,php,Php,我明白了: 警告:number_format()要求参数1为float,第205行D:\xampp\htdocs\website\foodpoint\welcome.php中给出的字符串 这是我在205行的代码: <?php $tot_tapas = number_format($number_of_persons, 2) * number_format($tapas, 2) ?> <?php $details = json_decode($obj->details);

我明白了:

警告:number_format()要求参数1为float,第205行D:\xampp\htdocs\website\foodpoint\welcome.php中给出的字符串

这是我在205行的代码:

<?php
$tot_tapas = number_format($number_of_persons, 2) * number_format($tapas, 2)
?>

<?php
$details = json_decode($obj->details);
$tapas = $number_of_persons = '';
foreach ($details->items as $item) {
    foreach ($item->extras as $extra) {
        // new
        $tapas = $item->service_price;
        $number_of_persons = $item->number_of_persons;
        // new
        ?>
        <div class="row">
            <div class="col kolom1">
                <?php echo $extra->quantity; ?>x <?php echo $extra->title; ?>
            </div>
            <div class="col kolom2">
                P/s : €<?php echo $extra->price; ?>
            </div>
            <div class="col kolom3">
                <?php $total = $extra->quantity * $extra->price; ?>
                <?php echo '€' . number_format($total, 2); ?>
            </div>
        </div>
        <hr>
        <?php
        
    }
}
?>

x
P/s:€


错误告诉您,
number\u format()
函数的第一个参数不是浮点(它应该是)

您必须将变量转换为float。你可以用它,也可以用它


在您的情况下,它是
$number\u of_persons
$tapas
或两者都是如果您确定变量的值为数字,则可以执行以下操作:

$tot_tapas = number_format((int) $number_of_persons, 2)  * number_format((int) $tapas, 2)
$tot_tapas = float($number_of_persons) * float($tapas);
$tot_tapas = number_format((float)$tot_tapas , 2, '.', '');
魔法是类型杂耍,您可以将其转换为int。请阅读以下内容:

但是,您应该确保变量的类型是integer,并且没有字符串值

$number_of_persons = 4;
$tapas = 2; 
// => OK

$number_of_persons = "4";
$tapas = "two tapas 2"; 
// => Warning: number_format() expects parameter 1 to be float, string given
您可以使用floatval()。
但我想你可以找到哪一个不是浮点数或塔帕。使用gettype()查看哪一个是字符串并修复它。

如果确定变量的值为数字,则可以执行以下操作:

$tot_tapas = number_format((int) $number_of_persons, 2)  * number_format((int) $tapas, 2)
$tot_tapas = float($number_of_persons) * float($tapas);
$tot_tapas = number_format((float)$tot_tapas , 2, '.', '');

该消息告诉您
$number\u of_persons
$tapas
是一个字符串,而不是浮点数。你能显示你定义/分配
$number\u of_persons
$tapas
的代码吗?不知道你为什么在这里使用
number\u format()
,也许你只想确保这两个数字是2位小数。@kerbh0lz我会在你正在使用的后期添加PHP版本中添加它;tapas的定义,或者至少从何处获取,并简要描述您试图实现的目标。不,您应该在number_format()的第一个参数上使用floatval。像这个数字格式(floatval($first_-param),$second_-param)解析错误:语法错误,D:\xampp\htdocs\website\foodpoint\welcome.php第205行的“数字格式”(T_-STRING),预期为“'),仍然得到这个
@Marvin第一个!number_format()的参数:
number_format(floatval($number_of_persons),2)*number_format(floatval($tapas),2)
@Marvin,您显示给我们的所有错误都非常明确。如果你不是以英语为母语的人,你可以用任何在线翻译工具轻松翻译。它应该能直接帮助你。