我的 PHP 代码运行时没有错误,但会无限加载
My PHP code runs with no errors but infinitely loads
我是 PHP 的新手,我尝试编写一个程序,在其中找到与定义的变量(此处为 $input)最接近的 2 次方数。
令人惊奇的是,它运行起来没有任何错误,但可以无限加载!我试过php_codesniffer,但是没有显示任何错误,所以我们可以确定它不是语法错误。我也尝试重新启动 apache,但没有帮助。为了确保它不是来自 apache,我尝试了 运行 我的其他代码,它们在 apache 服务器上 运行 没有问题。这是我的代码:
<?php
$no = 5443.985;
// this function gets a nomber $input and returns the nearest number to $input between $checkingNo_one & $checkongNo_two
function check_nearer($input, $checkingNo_one, $checkingNo_two)
{
if (($input - $checkingNo_one) >= ($checkingNo_two - $input)) {
return $checkingNo_two;
} else {
return $checkingNo_one;
}
}
$twoPower = 1;
while ($twoPower < $no) {
$twoPower **= 2;
}
$twoPower_alt = $twoPower ** 2; // to see if the number after our input isn't nearer to it
$returningNo = check_nearer($no, $twoPower, $twoPower_alt);
echo $returningNo;
你怎么看?
只需将$twoPower = 1;
改为$twoPower = 2;
;因为你试图将 1 乘以它本身,它永远是 1 并且永远不会结束:
while ($twoPower < $no) {
$twoPower **= 2;
}
我是 PHP 的新手,我尝试编写一个程序,在其中找到与定义的变量(此处为 $input)最接近的 2 次方数。
令人惊奇的是,它运行起来没有任何错误,但可以无限加载!我试过php_codesniffer,但是没有显示任何错误,所以我们可以确定它不是语法错误。我也尝试重新启动 apache,但没有帮助。为了确保它不是来自 apache,我尝试了 运行 我的其他代码,它们在 apache 服务器上 运行 没有问题。这是我的代码:
<?php
$no = 5443.985;
// this function gets a nomber $input and returns the nearest number to $input between $checkingNo_one & $checkongNo_two
function check_nearer($input, $checkingNo_one, $checkingNo_two)
{
if (($input - $checkingNo_one) >= ($checkingNo_two - $input)) {
return $checkingNo_two;
} else {
return $checkingNo_one;
}
}
$twoPower = 1;
while ($twoPower < $no) {
$twoPower **= 2;
}
$twoPower_alt = $twoPower ** 2; // to see if the number after our input isn't nearer to it
$returningNo = check_nearer($no, $twoPower, $twoPower_alt);
echo $returningNo;
你怎么看?
只需将$twoPower = 1;
改为$twoPower = 2;
;因为你试图将 1 乘以它本身,它永远是 1 并且永远不会结束:
while ($twoPower < $no) {
$twoPower **= 2;
}