PHP

How to convert number to String in PHP

In this method, to convert an integer to string, write (string) before the integer, and PHP will convert it to string type.

<?php
error_reporting(0);
function convertNumberToWord($num = false)
{
    $num = str_replace(array(',', ' '), '' , trim($num));
    if(! $num) {
        return false;
    }
    $num = (int) $num;
    $words = array();
    $list1 = array('', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven',
        'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'
    );
    $list2 = array('', 'ten', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety', 'hundred');
    $list3 = array('', 'thousand', 'million', 'billion'
    );
    $num_length = strlen($num);
    $levels = (int) (($num_length + 2) / 3);
    $max_length = $levels * 3;
    $num = substr('00' . $num, -$max_length);
    $num_levels = str_split($num, 3);
    for ($i = 0; $i < count($num_levels); $i++) {
        $levels--;
        $hundreds = (int) ($num_levels[$i] / 100);
        $hundreds = ($hundreds ? ' ' . $list1[$hundreds] . ' hundred' . ( $hundreds == 1 ? '' : 's' ) . ' ' : '');
        $tens = (int) ($num_levels[$i] % 100);
        $singles = '';
        if ( $tens < 20 ) {
            $tens = ($tens ? ' ' . $list1[$tens] . ' ' : '' );
        } else {
            $tens = (int)($tens / 10);
            $tens = ' ' . $list2[$tens] . ' ';
            $singles = (int) ($num_levels[$i] % 10);
            $singles = ' ' . $list1[$singles] . ' ';
        }
        $words[] = $hundreds . $tens . $singles . ( ( $levels && ( int ) ( $num_levels[$i] ) ) ? ' ' . $list3[$levels] . ' ' : '' );
    }
    $commas = count($words);
    if ($commas > 1) {
        $commas = $commas - 1;
    }
    $data= implode(' ', $words);
	return $data." ".'only';
}
if(isset($_POST['confirm'])){
echo "<p style=color:red>".convertNumberToWord($_POST['number'])."</p><br>";
}
?>
<form method="post">
 Enter Number: <input type="" name="number" value='' required /></br></br>
 <input type="submit" name="confirm" value='Convert Into String' />
</form>
View Demo

Recommended Tutorials for you


Related posts

String functions

Anuj Kumar

PHP – PECL

Anuj Kumar

How to check Email and username availabilty live using jquery/ajax

Anuj Kumar

How to delete multiple records in PHP

Anuj Kumar

Implode() and Explode() Function in PHP

Anuj Kumar

How to get Current Indian time in PHP

Anuj Kumar

PHP Prepared Statements

Anuj Kumar

How to send Mail Using PHP

Anuj Kumar

2 comments

Vara Prasad November 10, 2018 at 5:26 am

i have small doubt in php that is how can i update student year like 1, 2, 3,4. it will update automatically when complete one year and add 3000 rs amount in their balance box.

Reply
Vara Prasad September 3, 2017 at 11:05 pm

i have small doubt in php that is how can i update student year like 1, 2, 3,4. it will update automatically when complete one year and add 3000 rs amount in their balance box.

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.