Jump to content

New conversion help please


simflex

Recommended Posts

Hello all,

 

I thought this would be pretty simple.

 

I need to convert numbers 1 to 15 to words and then write out the words.

 

e.g, 1 converted to One, 2 converted to Two, etc

 

Here is the code. I am not getting any errors; just not seeing results being displayed.

 

Thanks a lot for your assistance.

 

<?

// numbers to words function

// example: 7 -> seven

// works with 0 to 10 inclusive

//Anything below 0 or greater than 10 invites an error

function numbertowords($str){

 

$words = array(1 => "one",

                2 => "two",

                3 => "three",

                4 => "four",

                5 => "five",

                6 => "six",

                7 => "seven",

                8 => "eight",

                9 => "nine",

                10 => "ten",

                11 => "eleven",

                12 => "twelve",

                13 => "thirteen",

                14 => "fourteen",

                15 => "fiften):

                );

 

foreach($words as $key => $val){

  if(strtolower($str) == $key || $str == $val){

    return $val;

                              }

                        }

        return '';// returns nothing

  echo numbertowords($words),"<br>";

}

 

?>

 

Link to comment
Share on other sites

Thank you very much for your assistance.

 

I believe I have made the change but still blank screen.

 

I am very new to this and your assistance is greatly appreciated.

 

<?

// numbers to words function

// example: 7 -> seven

// works with 0 to 15 inclusive

//Anything below 0 or greater than 15 invites an error

function numbertowords($str){

 

$words = array(1 => "one",

                2 => "two",

                3 => "three",

                4 => "four",

                5 => "five",

                6 => "six",

                7 => "seven",

                8 => "eight",

                9 => "nine",

                10 => "ten",

                11 => "eleven",

                12 => "twelve",

                13 => "thirteen",

                14 => "fourteen",

                15 => "fifteen" );

 

foreach($words as $key => $val){

  if(strtolower($str) == $key || $str == $val){

    return $val;

                              }

                        }

        return '';// returns nothing

 

}

  echo numbertowords($words),"<br>";

?>

 

Link to comment
Share on other sites

Your current code produces this error -

 

Notice: Undefined variable: words in your_file.php on line 32

 

The parameter you are supplying in the function call is not set to any value, so you get nothing out.

 

You should also be developing and debugging php code on a system with error_reporting set to E_ALL and display_errors set to ON so that php will help you. You will save a TON of time.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.