For循环未针对浮点值运行 [英] For loop is not running for float values

查看:47
本文介绍了For循环未针对浮点值运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的for循环

i have a for loop like below

<?php 

 for($i=0;$i<=10;$i+0.4){

 echo $i."<br>";
 }

 ?>

此代码显示i的值直到9.6而不是10.

this code prints the value of i till 9.6 not 10.

为什么最后返回i = 10的值.

why it returns the value of i=10 at last.

推荐答案

使用+=对其进行递增,而不仅仅是加号.现在,对我来说这是一个无限循环.

Use += to increment it, instead of just plus. As it is now, its an infinite loop for me.

由于某种原因,PHP无法在循环中与其他类型一起正常工作.

For some reason PHP doesn't work properly with different types in loops.

这应该可以工作

for($i=0;$i<=100;$i+=4){
   echo $i/10."<br>";
 }

这是var_dump

int(0)

float(0.4)

float(0.8)

float(1.2)

float(1.6)

int(2)

float(2.4)

float(2.8)

float(3.2)

float(3.6)

int(4)

float(4.4)

float(4.8)

float(5.2)

float(5.6)

int(6)

float(6.4)

float(6.8)

float(7.2)

float(7.6)

int(8)

float(8.4)

float(8.8)

float(9.2)

float(9.6)

int(10)

这可能是因为自动广播PHP正在这样做导致

That's probably the auto-casting PHP is doing that is causing this

这篇关于For循环未针对浮点值运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆