問題描述
我想知道將值從一種類型轉換為另一種類型的最佳方法是什么.
I was wondering what is the best way to typecast a value from one type to another.
我們應該使用哪種變體:
Which variant should we use:
intval($value);
settype($value, 'int')
(int)$value
所有這些都產生相同的結果.
All of them produce same result.
推薦答案
(int)$value
與 intval($value)
和 settype($value, 'int')
相比,節省了一個函數調用.
saves one function call compares to intval($value)
and settype($value, 'int')
.
而且 (int)$value
已經足夠干凈了,所以我更喜歡這種方式.
And (int)$value
is clean enough, so I prefer this way.
When you need to use intval($value)
是當你需要使用指定的基數進行轉換時(默認為基數 10).intval 接受第二個參數作為轉換的基礎.
When you need to use intval($value)
is that when you need to use the specified base for the conversion (the default is base 10). intval accepts a second parameter for the base for the conversion.
這篇關于PHP 類型轉換的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!