2024 日本 PHP 研討會

strval

(PHP 4, PHP 5, PHP 7, PHP 8)

strval取得變數的字串值

說明

strval(混合 $value): 字串

取得變數的字串值。有關轉換為字串的更多資訊,請參閱字串的說明文件。

此函式不會對回傳值進行格式化。如果您正在尋找一種將數值格式化為字串的方法,請參閱 sprintf()number_format()

參數

value

要被轉換成 字串 的變數。

value 可以是任何純值類型、null,或是實作了 __toString() 方法的 物件。您不能對陣列或未實作 __toString() 方法的物件使用 strval()

返回值

value字串 值。

範例

範例 #1 使用 PHP 魔術方法 __toString()strval() 範例。

<?php
class StrValTest
{
public function
__toString()
{
return
__CLASS__;
}
}

// 顯示 'StrValTest'
echo strval(new StrValTest);
?>

參見

新增筆記

使用者貢獻的筆記 7 則筆記

Mark Clements
8 年前
關於此函式如何隨著時間推移而變化的說明,特別是以下陳述:

> 您不能對陣列或未實作 __toString() 方法的物件使用 strval()。


== 陣列 ==

在 PHP 5.3 及更低版本中,strval(array(1, 2, 3)) 會返回字串 "Array",而不會發生任何錯誤。

從 5.4 及更高版本開始,返回值保持不變,但現在您會收到一個通知級別的錯誤:「陣列轉字串」。

== 物件 ==

對於未實作 __toString() 的物件,行為有所不同

PHP 4: "Object"
PHP 5 < 5.2: "Object id #1"(數字顯然會有所不同)
PHP >= 5.2: 可捕捉的致命錯誤:類別 X 的物件無法轉換為字串
Hayley Watson
17 年前
從 PHP 5.2 開始,strval() 將返回物件的字串值,並呼叫其 __toString() 方法來決定該值為何。
Tom Nicholson
20 年前
如果您想將一個整數轉換成英文單詞字符串,例如 29 -> twenty-nine,這裡有一個函數可以做到。

關於 fmod() 的使用說明
我使用浮點數 fmod() 而不是 % 運算符,因為 % 會將運算元轉換為 int,從而損壞 [-2147483648, 2147483647] 範圍之外的值。

我沒有處理「billion」,因為這個詞代表 10e9 或 10e12,取決於你問誰。

如果參數不代表整數,則該函數返回 '#'。

<?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;
}

?>

用法
<?php
echo '目前有 '. int_to_words($count) . ' 位成員登入。';
?>
php at ianco dot co dot uk
19 年前
我很驚訝

(string)"0" == (string)"0.00"

的結果為真。使用 strval 和單引號也是一樣。
使用 === 可以避免這個問題。

為什麼這很重要?令人難以置信的是,我的一個供應商在其庫存文件中使用 0 表示標準折扣,而使用 0.00 表示無折扣。
kendsnyder+phpnet at gmail dot com
17 年前
將大型浮點數轉換為字符串的唯一方法是使用 printf('%0.0f',$float); 而不是 strval($float); (php 5.1.4)。

// strval() 會在 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

// 完整轉換
printf('%0.0f',pow(2,50)); // 112589906846624
echo sprintf('%0.0f',pow(2,50)); // 112589906846624
NyctoFixer at gmail dot com
17 年前
在 PHP 5.1.4 中(我沒有在更高版本中測試過),strval 函數在遇到對象時不會嘗試調用 __toString 方法。這個簡單的包裝函數將為您處理這種情況

<?

/**
* 返回變量的字符串值
*
* 這與 strval 不同,如果給定一個對象,它會調用 __toString
* 且該對象具有該方法
*/
function stringVal ($value)
{
// 我們使用 get_class_methods 而不是 method_exists 來確保 __toString 是一個公共方法
if (is_object($value) && in_array("__toString", get_class_methods($value)))
return strval($value->__toString());
else
return strval($value);
}

?>
Steve Ball
19 年前
看起來一個被視為無符號大整數(32 位元),另一個被視為有符號大整數(已發生溢位/下溢)。

2326201276 - (-1968766020) = 4294967296.
To Top