Skip to content

Instantly share code, notes, and snippets.

@Guley
Created September 4, 2024 11:47
Show Gist options
  • Save Guley/17a168a4f43b772700dac62e1ba45443 to your computer and use it in GitHub Desktop.
Save Guley/17a168a4f43b772700dac62e1ba45443 to your computer and use it in GitHub Desktop.
Convert any number to word using php
function numbertoword($number) {
$units = array('', 'One', 'Two', 'Three', 'Four',
'Five', 'Six', 'Seven', 'Eight', 'Nine');
$tens = array('', 'Ten', 'Twenty', 'Thirty', 'Forty',
'Fifty', 'Sixty', 'Seventy', 'Eighty',
'Ninety');
$special = array('Eleven', 'Twelve', 'Thirteen',
'Fourteen', 'Fifteen', 'Sixteen',
'Seventeen', 'Eighteen', 'Nineteen');
$words = '';
if ($number < 10) {
$words .= $units[$number];
} elseif ($number < 20) {
$words .= $special[$number - 11];
} else {
$words .= $tens[(int)($number / 10)] . ' '
. $units[$number % 10];
}
return $words;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment