tags:

views:

98

answers:

2

In PHP is it better to convert a value to an integer using the syntax

(int)$value

or

intval($value)

the question is also be relevant to string, bool, float

+2  A: 

There is negligible difference between the two. Even with completely unrealistic scenarios (100k's loops) you save milliseconds at best.

Do milliseconds matter? Yes. Is this ever going to be the biggest candidate for optimization in your app? No.

Pick the one with the syntax you prefer.

Andrew Grant
Gotta love PHP... 15 ways to do one thing =)
Andrew
+2  A: 

Using intval() has the overhead of a function call, but will allow you to use a base other than 10.

(int) is faster.

Before anyone says the speed doesn't matter - if you're doing this a lot (1000s or 10,000s of times) and running a profiler, extra function calls WILL make a difference.

Greg
I've heard this before to... to quote from memory... "(int) is a language construct, so its faster than the function call"
SkippyFire
User-space function calls are expensive. Native calls--such as intval()--are far less so. In this case, I looped both 10k times and the diff is 0.0032 seconds. When novice devs read advice like yours they optimize this stuff when they should spend more time on the REAL time-sinks in their apps.
Encoderer