search for in the  
<settypeunserialize>
Last updated: Thu, 19 May 2005

strval

(PHP 3, PHP 4, PHP 5)

strval -- Get string value of a variable

Description

string strval ( mixed var )

Returns the string value of var. See the documentation on string for more information on converting to string.

var may be any scalar type. You cannot use strval() on arrays or objects.

See also floatval(), intval(), settype() and Type juggling.



User Contributed Notes
strval
Redbeard
24-Aug-2004 04:40
You can also use the PEAR package Numbers_Words, which handles many other languages.
Tom Nicholson
28-Apr-2004 11:13
If you want to convert an integer into an English word string, eg. 29 -> twenty-nine, then here's a function to do it.

Note on use of fmod()
  I used the floating point fmod() in preference to the % operator, because % converts the operands to int, corrupting values outside of the range [-2147483648, 2147483647]

I haven't bothered with "billion" because the word means 10e9 or 10e12 depending who you ask.

The function returns '#' if the argument does not represent a whole number.

<?php
$nwords
= array( "zero", "one", "two", "three", "four", "five", "six", "seven",
                  
"eight", "nine", "ten", "eleven", "twelve", "thirteen",
                  
"fourteen", "fifteen", "sixteen", "seventeen", "eighteen",
                  
"nineteen", "twenty", 30 => "thirty", 40 => "forty",
                  
50 => "fifty", 60 => "sixty", 70 => "seventy", 80 => "eighty",
                  
90 => "ninety" );

function
int_to_words($x) {
   global
$nwords;

   if(!
is_numeric($x))
    
$w = '#';
   else if(
fmod($x, 1) != 0)
    
$w = '#';
   else {
     if(
$x < 0) {
        
$w = 'minus ';
        
$x = -$x;
     } else
        
$w = '';
    
// ... now $x is a non-negative integer.

    
if($x < 21// 0 to 20
        
$w .= $nwords[$x];
     else if(
$x < 100) {  // 21 to 99
        
$w .= $nwords[10 * floor($x/10)];
        
$r = fmod($x, 10);
         if(
$r > 0)
          
$w .= '-'. $nwords[$r];
     } else if(
$x < 1000) {  // 100 to 999
        
$w .= $nwords[floor($x/100)] .' hundred';
        
$r = fmod($x, 100);
         if(
$r > 0)
          
$w .= ' and '. int_to_words($r);
     } else if(
$x < 1000000) {  // 1000 to 999999
        
$w .= int_to_words(floor($x/1000)) .' thousand';
        
$r = fmod($x, 1000);
         if(
$r > 0) {
          
$w .= ' ';
           if(
$r < 100)
              
$w .= 'and ';
          
$w .= int_to_words($r);
         }
     } else {   
//  millions
        
$w .= int_to_words(floor($x/1000000)) .' million';
        
$r = fmod($x, 1000000);
         if(
$r > 0) {
          
$w .= ' ';
           if(
$r < 100)
              
$word .= 'and ';
          
$w .= int_to_words($r);
         }
     }
   }
   return
$w;
}

?>

Usage:
<?php
echo 'There are currently '. int_to_words($count) . ' members logged on.';
?>

<settypeunserialize>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT