0

I have this php code:

<?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;
?>

The num value is equal to 1234567890. When I refresh the page, the value of $i only goes from 10 -> 9 -> 8 -> 7 -> 6 and then suddenly stops. Why would the loop stop running?

6
  • 1
    What is $numarray supposed to be?
    – Jack
    Apr 22, 2012 at 8:27
  • Oh it's the value of $_GET['num'].
    – Zerium
    Apr 22, 2012 at 8:28
  • 1
    I don't know php..but aren't you incrementing v and decrementing i in the loop..so now they meet at half way point?
    – Naveen
    Apr 22, 2012 at 8:29
  • I know but what is the value of $_GET['num']?
    – Jack
    Apr 22, 2012 at 8:29
  • It says down there: 1234567890. I'm using this as a number-to-words converter, and I wanted to do it with numbers less than 10 billion and bigger than 1 billion.
    – Zerium
    Apr 22, 2012 at 8:30

3 Answers 3

1

You try to check each digit:

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

But then you decrease $i in the loop:

$i = $i - 1;

That's why you see less loops than you expected.

0
1

I hope below post will help you achieve what you're looking for:

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

1
  • I looked at your link, and it was awesome! Thank you!
    – Zerium
    Apr 23, 2012 at 2:40
0

not optimal - but change

  $i = strlen($numarray);

to

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

and

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

to

  for ($v = 1; $v <= $count; $v++)
3
  • ... why did someone -1 this- when its the correct answer? it solves the problem?
    – Laurence
    Apr 22, 2012 at 8:40
  • I removed your -1 - but that's all I can do, sorry about that.
    – Zerium
    Apr 23, 2012 at 2:34
  • thats ok - i was just confused why it was -1 when it solved your problem :) all good
    – Laurence
    Apr 23, 2012 at 2:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.