开发者

performance about string convert to int

开发者 https://www.devze.com 2023-04-05 19:11 出处:网络
If i have sting 开发者_Python百科like $str = \'515\'; I want convert it to int, is better use $str = $str * 1;

If i have sting 开发者_Python百科like

$str = '515';

I want convert it to int, is better use

$str = $str * 1;

than use

$str = intval($str);

which performance is better?


When you use $str = $str * 1, $str will first cast into an integer then plus 1, so it is one step more.

Besides, $str = intval($str); is much more readable than $str = $str * 1;,

You could also just use casting by $str = (int)$str.


Casting the value using (int) should be the quickest option as intval() invokes a function (which has a small performance overhead)

$str = (int)$str;

see http://wiki.phpbb.com/Best_Practices:PHP#Typecasting for more information

0

精彩评论

暂无评论...
验证码 换一张
取 消