本篇文章給大家介紹一下PHP使用strval()函數的方法。有一定的參考價值,有需要的朋友可以參考一下,希望對大家有所幫助。

PHP如何使用strval()函數?
strval()函數是PHP中的內置函數, 用于將任何標量值(字符串, 整數或雙精度)轉換為字符串。我們不能在數組或對象上使用strval(), 如果應用了該函數, 則此函數僅返回要轉換的值的類型名稱。
語法:
strval( $variable )
參數:此函數接受單個參數$變量。此參數表示我們要轉換為字符串的值
返回值:此函數返回一個字符串。該字符串是通過類型轉換作為參數傳遞給它的變量的值生成的。
下面的程序說明了strval()函數。
程序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視頻教程
站長資訊網