Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 循环突然停止迭代?_Php_Loops - Fatal编程技术网

Php 循环突然停止迭代?

Php 循环突然停止迭代?,php,loops,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 =&g

我有以下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开始,然后突然停止。为什么循环停止运行?

您尝试检查每个数字:

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

这就是为什么您看到的循环比您预期的要少。

不是最佳的,而是变化

  $i = strlen($numarray);


for($v=1;$v我希望下面的帖子能帮助你实现你想要的目标:


什么是
$numaray
呢?哦,这是
$\u GET['num']
的值。我不知道php。但是你不是在循环中增加v和减少I吗。所以现在它们在中途相遇了吗?我知道,
$\u GET['num']的值是多少
?下面写着:
1234567890
。我把它用作一个数字到单词的转换器,我想用小于100亿和大于10亿的数字来做这件事……为什么有人-1这个-当它是正确的答案时?它解决了问题?我删除了你的-1-但我只能这么做,对不起。没关系-我只是被欺骗了当它解决了你的问题时,它为什么是-1:)好吧,我看了你的链接,真是太棒了!非常感谢。
  $i = strlen($numarray);
 $i = strlen($numarray);
 $count = $i;
 for ($v = 1; $v <= $i; $v++)
  for ($v = 1; $v <= $count; $v++)