simflex Posted March 6, 2010 Share Posted March 6, 2010 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>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/194310-new-conversion-help-please/ Share on other sites More sharing options...
Stephen Posted March 6, 2010 Share Posted March 6, 2010 You're missing a double quote in your array: 15 => "fiften): to 15 => "fiften" Quote Link to comment https://forums.phpfreaks.com/topic/194310-new-conversion-help-please/#findComment-1022209 Share on other sites More sharing options...
simflex Posted March 6, 2010 Author Share Posted March 6, 2010 Thanks a lot for the prompt response but it didn't fix the problem. I would like to be able to see the results displayed on the screen. Quote Link to comment https://forums.phpfreaks.com/topic/194310-new-conversion-help-please/#findComment-1022210 Share on other sites More sharing options...
PFMaBiSmAd Posted March 6, 2010 Share Posted March 6, 2010 The closing brace } for the function definition is at the end of the code. The current call to the function is inside of the function definition. Quote Link to comment https://forums.phpfreaks.com/topic/194310-new-conversion-help-please/#findComment-1022211 Share on other sites More sharing options...
simflex Posted March 6, 2010 Author Share Posted March 6, 2010 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>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/194310-new-conversion-help-please/#findComment-1022214 Share on other sites More sharing options...
PFMaBiSmAd Posted March 6, 2010 Share Posted March 6, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/194310-new-conversion-help-please/#findComment-1022215 Share on other sites More sharing options...
simflex Posted March 6, 2010 Author Share Posted March 6, 2010 Even after I defined $words and assigned a value to it, it still shows nothing, man! Quote Link to comment https://forums.phpfreaks.com/topic/194310-new-conversion-help-please/#findComment-1022217 Share on other sites More sharing options...
herghost Posted March 6, 2010 Share Posted March 6, 2010 because you are using short open tags, replace <? with <?php Quote Link to comment https://forums.phpfreaks.com/topic/194310-new-conversion-help-please/#findComment-1022219 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.