本篇文章給大家介紹一下PHP使用strval()函數(shù)的方法。有一定的參考價值,有需要的朋友可以參考一下,希望對大家有所幫助。
PHP如何使用strval()函數(shù)?
strval()函數(shù)是PHP中的內(nèi)置函數(shù), 用于將任何標量值(字符串, 整數(shù)或雙精度)轉(zhuǎn)換為字符串。我們不能在數(shù)組或?qū)ο笊鲜褂胹trval(), 如果應用了該函數(shù), 則此函數(shù)僅返回要轉(zhuǎn)換的值的類型名稱。
語法:
strval( $variable )
參數(shù):此函數(shù)接受單個參數(shù)$變量。此參數(shù)表示我們要轉(zhuǎn)換為字符串的值
返回值:此函數(shù)返回一個字符串。該字符串是通過類型轉(zhuǎn)換作為參數(shù)傳遞給它的變量的值生成的。
下面的程序說明了strval()函數(shù)。
程序1:
<?php $var_name = 32.360; // prints the value of above variable // as a string echo strval ( $var_name ); ?>
輸出如下:
32.36
程序2:
<?php class lsbin { public function __toString() { // returns the class name return __CLASS__ ; } } // prints the name of above class as // a string echo strval ( new lsbin); ?>
輸出如下:
lsbin
程序3:
<?php // Program to illustrate the strval() function // when an array is passed as parameter // Input array $arr = array (1, 2, 3, 4, 5); // This will print only the type of value // being converted i.e. 'Array' echo strval ( $arr ); ?>
推薦學習:php視頻教程