php类型的函数声明中的错误参数声明
案例:图书馆使用这样的代码:
function setUnitPrice(?int $unitPrice)
{
return $unitPrice;
}
当我喜欢这样的代码:
$var1 = 0.58;
$var1 = $var1 * 100;
echo setUnitPrice($var1);
它以57为57,php 7.2 使用PHP 8显示警告 弃用:从Float 57.9999999999999到INT的隐性转换,
我从来没有需要在PHP中绕过Float数字,可以解释这一点吗? 要解决此问题,需要具有Intaval(圆($ var1)),但这没有任何意义
Case: library uses code like this:
function setUnitPrice(?int $unitPrice)
{
return $unitPrice;
}
and when I do like this:
$var1 = 0.58;
$var1 = $var1 * 100;
echo setUnitPrice($var1);
it comes out as 57 with php 7.2
with php 8 it shows warning of
Deprecated: Implicit conversion from float 57.99999999999999 to int loses precision
I have never needed to round the float numbers in php before, can anybody explains this ?
to fix this, needs to have intaval(round($var1)) but this does not make any sense
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
$ var1已被解释为浮点。如果您做一个var_dump,您将看到。
但是你可以做
$var1 has been declerated as float. If you do a var_dump of it, you will see.
But you can do