©
本文档使用
php.cn手册 发布
(PHP 4, PHP 5, PHP 7)
strval — 获取变量的字符串值
$var
)
返回 var
的 string 值。
参见 string 文档获取更多关于字符串转换的信息。
var
可以是任何标量类型。不能将
strval() 用于数组或对象。
参见 floatval() 、 intval() 、 settype() 和类型戏法。
[#1] Hayley Watson [2007-08-21 20:53:49]
As of PHP 5.2, strval() will return the string value of an object, calling its __toString() method to determine what that value is.
[#2] NyctoFixer at gmail dot com [2007-06-11 12:19:48]
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:
<?php
function stringVal ($value)
{
// We use get_class_methods instead of method_exists to ensure that __toString is a public method
if (is_object($value) && in_array("__toString", get_class_methods($value)))
return strval($value->__toString());
else
return strval($value);
}
?>
[#3] kendsnyder+phpnet at gmail dot com [2007-06-01 22:08:56]
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
[#4] portos_ze_retour at hotmail dot fr [2006-03-10 08:15:10]
In complement to Tom Nicholson's contribution, here is the french version (actually it's possible to change the language, but you should check the syntax ;) )
function int_to_words($x) {
global $nwords;
if(!is_numeric($x))
$w = '#';
else if(fmod($x, 1) != 0)
$w = '#';
else {
if($x < 0) {
$w = $nwords['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)] .' '.$nwords['hundred'];
$r = fmod($x, 100);
if($r > 0)
$w .= ' '.$nwords['separator'].' '. int_to_words($r);
} else if($x < 1000000) { // 1000 to 999999
$w .= int_to_words(floor($x/1000)) .' '.$nwords['thousand'];
$r = fmod($x, 1000);
if($r > 0) {
$w .= ' ';
if($r < 100)
$w .= $nwords['separator'].' ';
$w .= int_to_words($r);
}
} else { // millions
$w .= int_to_words(floor($x/1000000)) .' '.$nwords['million'];
$r = fmod($x, 1000000);
if($r > 0) {
$w .= ' ';
if($r < 100)
$word .= $nwords['separator'].' ';
$w .= int_to_words($r);
}
}
}
return $w;
}
// Usage in English
$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" , "hundred" => "hundred", "thousand"=> "thousand", "million"=>"million",
"separator"=>"and", "minus"=>"minus");
echo 'There are currently '. int_to_words(-120223456) . ' members logged on.<br>';
//Utilisation en Francais
$nwords = array( "z??ro", "un", "deux", "trois", "quatre", "cinq", "six", "sept",
"huit", "neuf", "dix", "onze", "douze", "treize",
"quatorze", "quinze", "seize", "dix-sept", "dix-huit",
"dix-neuf", "vingt", 30 => "trente", 40 => "quarante",
50 => "cinquante", 60 => "soixante", 70 => "soixante-dix", 80 => "quatre-vingt",
90 => "quatre-vingt-dix" , "hundred" => "cent", "thousand"=> "mille", "million"=>"million",
"separator"=>"", "minus"=>"moins");
echo 'Il y a actuellement '. int_to_words(-120223456) . ' membres connect??s.<br>';
[#5] anthony dot parsons at manx dot net [2006-01-09 09:59:26]
If you have to compare object variables like this be careful not to make a typo, or you could end up calling __set() -
<?php
if ( $user->password == $user2->password )
if ( $user->password = $user2->password )
?>
To avoid that ever happening, do it like this:
<?php
if ( strval($user->password) == $user2->password )
?>
[#6] php at ianco dot co dot uk [2005-10-07 04:36:48]
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.
[#7] Steve Ball [2005-09-08 19:18:05]
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.
[#8] Tom Nicholson [2004-04-28 09:13:06]
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.';
?>