PHP: Hypertext Preprocessor (original) (raw)
strval
(PHP 4, PHP 5, PHP 7, PHP 8)
strval — Get string value of a variable
Description
Get the string value of a variable. See the documentation on string for more information on converting to string.
This function performs no formatting on the returned value. If you are looking for a way to format a numeric value as a string, please see sprintf() or number_format().
Parameters
value
The variable that is being converted to a string.
value
may be any scalar type, null, or an object that implements the __toString() method. You cannot use strval() on arrays or on objects that do not implement the__toString() method.
Return Values
The string value of value
.
Examples
Example #1 strval() example using PHP magic__toString() method.
<?php class StrValTest { public function __toString() { return __CLASS__; } }// Prints 'StrValTest' echo strval(new StrValTest); ?>
See Also
- boolval() - Get the boolean value of a variable
- floatval() - Get float value of a variable
- intval() - Get the integer value of a variable
- settype() - Set the type of a variable
- sprintf() - Return a formatted string
- number_format() - Format a number with grouped thousands
- Type juggling
- __toString()
Found A Problem?
8 years ago
`Some notes about how this function has changed over time, with regards the following statement:
You cannot use strval() on arrays or on objects that
do not implement the __toString() method.
== Arrays ==
In PHP 5.3 and below, strval(array(1, 2, 3)) would return the string "Array" without any sort of error occurring.
From 5.4 and above, the return value is unchanged but you will now get a notice-level error: "Array to string conversion".
== Objects ==
For objects that do not implement __toString(), the behaviour has varied:
PHP 4: "Object"
PHP 5 < 5.2: "Object id #1" (number obviously varies)
PHP >= 5.2: Catchable fatal error: Object of class X could not be converted to string
`
17 years ago
As of PHP 5.2, strval() will return the string value of an object, calling its __toString() method to determine what that value is.
21 years ago
`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.
"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 = -x=−x; } else $w = ''; // ... now xisanon−negativeinteger.if(x is a non-negative integer.if(xisanon−negativeinteger.if(x < 21) // 0 to 20 w.=w .= w.=nwords[$x]; else if($x < 100) { // 21 to 99 w.=w .= w.=nwords[10 * floor($x/10)]; r=fmod(r = fmod(r=fmod(x, 10); if($r > 0) w.=′−′.w .= '-'. w.=′−′.nwords[$r]; } else if($x < 1000) { // 100 to 999 w.=w .= w.=nwords[floor($x/100)] .' hundred'; r=fmod(r = fmod(r=fmod(x, 100); if($r > 0) w.=′and′.inttowords(w .= ' and '. int_to_words(w.=′and′.inttowords(r); } else if($x < 1000000) { // 1000 to 999999 w.=inttowords(floor(w .= int_to_words(floor(w.=inttowords(floor(x/1000)) .' thousand'; r=fmod(r = fmod(r=fmod(x, 1000); if($r > 0) { $w .= ' '; if($r < 100) $w .= 'and '; w.=inttowords(w .= int_to_words(w.=inttowords(r); } } else { // millions w.=inttowords(floor(w .= int_to_words(floor(w.=inttowords(floor(x/1000000)) .' million'; r=fmod(r = fmod(r=fmod(x, 1000000); if($r > 0) { $w .= ' '; if($r < 100) $word .= 'and '; w.=inttowords(w .= int_to_words(w.=inttowords(r); } } } return $w; }?>Usage:
`
19 years ago
`I can't help being surprised that
(string)"0" == (string)"0.00"
evaluates to true. It's the same with strval and single quotes.
=== avoids it.
Why does it matter? One of my suppliers, unbelievably, uses 0 to mean standard discount and 0.00 to mean no discount in their stock files.
`
kendsnyder+phpnet at gmail dot com ¶
17 years ago
`The only way to convert a large float to a string is to use printf('%0.0f',$float); instead of strval($float); (php 5.1.4).
// strval() will lose digits around pow(2,45);
echo pow(2,50); // 1.1258999068426E+015
echo (string)pow(2,50); // 1.1258999068426E+015
echo strval(pow(2,50)); // 1.1258999068426E+015
// full conversion
printf('%0.0f',pow(2,50)); // 112589906846624
echo sprintf('%0.0f',pow(2,50)); // 112589906846624
`
17 years ago
`As of PHP 5.1.4 (I have not tested it in later versions), the strval function does not attempt to invoke the __toString method when it encounters an object. This simple wrapper function will handle this circumstance for you:
__toString()); else return strval($value); } ?>`
19 years ago
`It seems that one is being treated as an unsigned large int (32 bit), and the other as a signed large int (which has rolled over/under).
2326201276 - (-1968766020) = 4294967296.
`