問(wèn)題描述
我想知道將值從一種類型轉(zhuǎn)換為另一種類型的最佳方法是什么.
I was wondering what is the best way to typecast a value from one type to another.
我們應(yīng)該使用哪種變體:
Which variant should we use:
intval($value);
settype($value, 'int')
(int)$value
所有這些都產(chǎn)生相同的結(jié)果.
All of them produce same result.
推薦答案
(int)$value
與 intval($value)
和 settype($value, 'int')
相比,節(jié)省了一個(gè)函數(shù)調(diào)用.
saves one function call compares to intval($value)
and settype($value, 'int')
.
而且 (int)$value
已經(jīng)足夠干凈了,所以我更喜歡這種方式.
And (int)$value
is clean enough, so I prefer this way.
When you need to use intval($value)
是當(dāng)你需要使用指定的基數(shù)進(jìn)行轉(zhuǎn)換時(shí)(默認(rèn)為基數(shù) 10).intval 接受第二個(gè)參數(shù)作為轉(zhuǎn)換的基礎(chǔ).
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.
這篇關(guān)于PHP 類型轉(zhuǎn)換的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!