Tagged: string

3

How to append a string in PHP

By using Concatenation assignment operator(.=) we can append a string with another string.

<?php
$string="Welcome to";
$string.="PHPGurukul";
echo $string;
?>

Output will be  : Welcome to PHPGurukul

How to Concatenate two or more string in PHP 3

How to Concatenate two or more string in PHP

We can concatenate two or more string in PHP by concatenation operator(“.”);
Example 1 :

<?php
$string1="Hello";
$string2="World !!";
$string3=$string1.$string2;
echo $string3;
?>

Output : HelloWorld !!
Example 2:  if you want to add space between two strings

<?php
$string1="Hello";
$string2="World !!";
$string3=$string1." ".$string2;
echo $string3;
?>

Output : Hello World !!
Example 3:  Concatenate more than two strings

<?php
$string1="Welcome to";
$string2="PHPGurukul";
$string3="Programming Blog";
$string4=$string1.$string2.$string3;
echo $string4;
?>

Output : Welcome to PHPGurukul Programming Blog

Convert Number to String in PHP 2

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>