3

If i have sting like

$str = '515';

I want convert it to int, is better use

$str = $str * 1;

than use

$str = intval($str);

which performance is better?

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

Comments

5

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.