Christopher
Christopher

Reputation:

To cast or to function call?

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

Upvotes: 2

Views: 173

Answers (2)

Andrew Grant
Andrew Grant

Reputation: 58786

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.

Upvotes: 3

Greg
Greg

Reputation: 321588

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.

Upvotes: 3

Related Questions