一个循环突然停止迭代?

时间:2012-04-22 08:22:56

标签: php loops

我有这个PHP代码:

<?php
$numarray = trim($_GET['num']);
$i = strlen($numarray);
$result = "";
$numneedarray = array(
90 => 'ninety',
80 => 'eighty',
70 => 'seventy',
60 => 'sixty',
50 => 'fifty',
40 => 'forty',
30 => 'thirty',
20 => 'twenty',
19 => 'nineteen',
18 => 'eighteen',
17 => 'seventeen',
16 => 'sixteen',
15 => 'fifteen',
14 => 'fourteen',
13 => 'thirteen',
12 => 'twelve',
11 => 'eleven',
10 => 'ten',
9 => 'nine',
8 => 'eight',
7 => 'seven',
6 => 'six',
5 => 'five',
4 => 'four',
3 => 'three',
2 => 'two',
1 => 'one'
);
for ($v = 1; $v <= $i; $v++) {
if ($i > 10) {
exit("Has to be 10-digit or less.");
}
if ($i == 3) {
    $result .= ", " . $numneedarray[$numarray[strlen($numarray) - 3]] . " hundred";
    if ($numarray % 100 == 0) {
    echo "Hi95";
    break;
    }
} elseif ($i == 2) {
    if ($numarray[$v] == 1) {
        $othernum = $numarray[strlen($numarray) - 1];
        $othernum2 = $numarray[strlen($numarray) - 2] * 10;
        $othernum3 = $othernum2 + $othernum;
        if (strlen($numarray) > 2) {
        $result .= " and ";
        }
        $result .= $numneedarray[$othernum3];
        echo "Hi107";
    } else {
        $othernum = $numarray[strlen($numarray) - 2] * 10;
        $othernum2 = $numarray[strlen($numarray) - 1];
        $result .= " and " . $numneedarray[$othernum] . " " . $numneedarray[$othernum2];
        echo "Hi112";
    }
}
if ($i == 10) {
    $digit = substr($numarray, 0, 1);
    $result .= $numneedarray[$digit] . " billion";
    if ($numarray % 1000000000 == 0) {
    break;
    }
} elseif ($i == 9) {
    $number = substr($numarray, 1, 3);
    $digit1 = substr($number, 0, 1);
    $digit1con = $numneedarray[$digit1] . " hundred";
    $digit2 = substr($number, 1, 1);
    $noneed = false;
    if ($digit2 != 1) {
    $digit2con = $numneedarray[$digit2 * 10];
    } else {
    $digit23 = substr($number, 1, 2);
    $digit23con = $numneedarray[$digit23];
    $noneed = true;
    }
        $digit3 = substr($number, -1);
        $digit3con = $numneedarray[$digit3];
    if ($noneed == true) {
        $result .= ", " . $digit1con . " and " . $digit23con . " million";
    } else {
        $result .= ", " . $digit1con . " and " . $digit2con . " " . $digit3con . " million";
    }
    if ($numarray % 100000000 == 0) {
    echo "Hi";
    break;
    }
} elseif ($i == 6) {
    $number = substr($numarray, 4, 3);
    $digit1 = substr($number, 0, 1);
    $digit1con = $numneedarray[$digit1] . " hundred";
    $digit2 = substr($number, 1, 1);
    $noneed = false;
    if ($digit2 != 1) {
    $digit2con = $numneedarray[$digit2 * 10];
    } else {
    $digit23 = substr($number, 1, 2);
    $digit23con = $numneedarray[$digit23];
    $noneed = true;
    }
        $digit3 = substr($number, -1);
        $digit3con = $numneedarray[$digit3];
    if ($noneed == true) {
        $result .= ", " . $digit1con . " and " . $digit23con . " thousand";
    } else {
        $result .= ", " . $digit1con . " and " . $digit2con . " " . $digit3con . " thousand";
    }
    if ($numarray % 100000 == 0) {
    echo "Hi89";
    break;
    }
} 
echo $i;
$i = $i - 1;
}
if (strlen($numarray) == 1) {
    echo $numneedarray[$numarray];
}
echo $result;
?>

num值等于1234567890。刷新页面时,$i的值仅从10 -> 9 -> 8 -> 7 -> 6开始,然后突然停止。为什么循环会停止运行?

3 个答案:

答案 0 :(得分:2)

您尝试检查每个数字:

for ($v = 1; $v <= $i; $v++) {

但是你在循环中减少了$i

$i = $i - 1;

这就是为什么你看到的循环少于预期。

答案 1 :(得分:1)

我希望以下帖子可以帮助您实现您的目标:

http://www.karlrixon.co.uk/writing/convert-numbers-to-words-with-php/

答案 2 :(得分:0)

不是最优的 - 但改变

  $i = strlen($numarray);

 $i = strlen($numarray);
 $count = $i;

 for ($v = 1; $v <= $i; $v++)

  for ($v = 1; $v <= $count; $v++)