2012-04-22 131 views
0

我有這樣的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開始,然後突然停止。爲什麼循環會停止運行?

+1

什麼是'$ numarray'應該是什麼? – Jack 2012-04-22 08:27:19

+0

哦,這是$ _GET ['num']'的值。 – think123 2012-04-22 08:28:42

+1

我不知道php ..但是你不增加v和遞減我在循環..現在他們見面在中途點? – Naveen 2012-04-22 08:29:12

回答

2

你嘗試檢查每一個數字:

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

但是當你在循環減少$i

$i = $i - 1; 

這就是爲什麼你看到比你預期的那樣循環。

+0

非常感謝! – think123 2012-04-22 08:34:13

0

不是最優的 - 但改變

$i = strlen($numarray); 

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

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

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

...爲什麼有人-1這 - 當它的正確答案?它解決了這個問題? – Laurence 2012-04-22 08:40:06

+0

我刪除了你的-1,但這就是我所能做的,對此感到遺憾。 – think123 2012-04-23 02:34:39

+0

多數民衆贊成在 - 我只是困惑,爲什麼它是-1時,它解決了你的問題:)一切都很好 – Laurence 2012-04-23 02:51:47