imvkmark
11/30/2018 - 3:15 AM

php:question.number_format

number_format 验证数字要点

请查看如下代码, 如果 $amount 传值是 1000, 会不会报错误, 原因是什么?

$amount = number_format($amount, 2);

$validator = \Validator::make([
    'amount' => $amount,
], [
    'amount' => 'required|numeric',
], [
    'amount.required' => '订单金额必须填写',
    'amount.numeric'  => '订单金额必须为数字',
]);

if ($validator->fails()) {
    return $this->setError($validator->errors());
}

答案: 会, number_format(1000, 2) 会被格式化为 1,000.00 , 不会被认为是合法的数字, 故而报错