<?php $foo = "1"; // $foo 是字符串類型 $bar = (int)$foo; // $bar 是整型 ?>
對于整型來說,強制轉換類型名稱為int或者integer。
2.內置函數方式
內置函數方式,就是使用PHP的內置函數intval進行變量的轉換操作。
<?php $foo = "1"; // $foo 是字符串類型 $bar = intval($foo); // $bar 是整型 ?>
intval函數的格式為:
int intval(mixed $var [, int $base]);
雖然PHP手冊中明確指出,intval()不能用于array和object的轉換。但是經過我測試,轉換array的時候不會出任何問題,轉換值為1,而不是想象中的0??峙率且驗樵赑HP內部,array類型的變量也被認為是非零值得緣故吧。
轉換object的時候,PHP會給出如下的 notice:
Object of html' target='_blank'>class xxxx could not be converted to int in xxxxx.php on line xx
轉換值同樣為1。
3.格式化字符串方式
格式化字符串方式,是利用sprintf的%d格式化指定的變量,以達到類型轉換的目的。
<?php $foo = "1"; // $foo 是字符串類型 $bar = sprintf("%d", $foo); // $bar 是字符串類型 ?>
嚴格意義上講sprintf的轉換結果還是string型,因此它不應該算是字符串轉化為整數的方式。但是經過他處理之后的字符串值確實已經成為了“被強制轉化為字符串類型的整數”。
對于一般的程序員來說,看到這里就算結束了,但對于部分變態程序員,則說說下面的性能測試:
1.基本功能測試
設定以下數組:
<?php $a[] = "1"; $a[] = "a1"; $a[] = "1a"; $a[] = "1a2"; $a[] = "0"; $a[] = array('4',2); $a[] = "2.3"; $a[] = "-1"; $a[] = new Directory(); ?>
使用三種方式依次轉化上面給出的數組中的元素,查看轉換情況。程序源代碼如下:
<?php $a[] = "1"; $a[] = "a1"; $a[] = "1a"; $a[] = "1a2"; $a[] = "0"; $a[] = array('4',2); $a[] = "2.3"; $a[] = "-1"; $a[] = new Directory(); // int print "(int)<br />"; foreach($a as $v) { var_dump((int)$v); print "<br />"; } // intval print "intval();<br />"; foreach($a as $v) { var_dump(intval($v)); print "<br />"; } // sprintf print "sprintf();<br />"; foreach($a as $v) { var_dump(sprintf("%d", $v)); print "<br />"; } ?>
程序的最終運行結果如下(已經去掉轉換object時出現的notice):
(int) int(1) int(0) int(1) int(1) int(0) int(1) int(2) int(-1) int(1) intval(); int(1) int(0) int(1) int(1) int(0) int(1) int(2) int(-1) int(1) sprintf(); string(1) "1" string(1) "0" string(1) "1" string(1) "1" string(1) "0" string(1) "1" string(1) "2" string(2) "-1" string(1) "1"
由此可以看出,三種轉換的結果是完全一樣的。那么從功能上講,3種方式都可以勝任轉換工作,那么接下來的工作就是看哪一種效率更高了。
2.性能測試
<?php $foo = "1';Select * ..."; ?> 獲取時間點的函數如下(用于獲取測試起始點和結束點,以計算消耗時間): <?php ** * Simple function to replicate PHP 5 behaviour */ function microtime_float() { list($usec, $sec) = explode(" ", microtime()); return ((float)$usec + (float)$sec); } ?>
測試過程是使用每種方式轉換變量$foo 1000000次(100萬次),并將各自的消耗時間輸出,總共進行三組測試,盡可能降低誤差。測試程序如下:
<?php function microtime_float() { list($usec, $sec) = explode(" ", microtime()); return ((float)$usec + (float)$sec); } $foo = "1';Select * ..."; // (int) $fStart = microtime_float(); for($i=0;$i<1000000;$i++) { $bar = (int)$foo; } $fEnd = microtime_float(); print "(int):" . ($fEnd - $fStart) . "s<br />"; // intval() $fStart = microtime_float(); for($i=0;$i<1000000;$i++) { $bar = intval($foo); } $fEnd = microtime_float(); print "intval():" . ($fEnd - $fStart) . "s<br />"; // sprintf() $fStart = microtime_float(); for($i=0;$i<1000000;$i++) { $bar = sprintf("%d", $foo); } $fEnd = microtime_float(); print "sprintf():" . ($fEnd - $fStart) . "s<br />"; ?>
最終的測試結果:
(int):0.67205619812012s intval():1.1603000164032s sprintf():2.1068270206451s (int):0.66051411628723s intval():1.1493890285492s sprintf():2.1008238792419s (int):0.66878795623779s intval():1.1613430976868s sprintf():2.0976209640503s
由此可以看出,使用強制類型轉換將字符串轉化為整數速度是最快的。
以上就是php如何將字符串轉化為整數及性能測試實例詳解的詳細內容,更多請關注 其它相關文章!
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。
新聞熱點
疑難解答