PHP typecasting PHP typecasting php php

PHP typecasting


(int)$value

saves one function call compares to intval($value) and settype($value, 'int').

And (int)$value is clean enough, so I prefer this way.

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.


I prefer using

settype($value,getType(intval((int)$value)));


The answer here is to use whatever reads "cleaner" to you. Any difference in speed is going to be so minor, that worrying about it is almost certain to cost you more time than you are liable to save. What will save time, however, is having code that you can read and understand in the future.

There's an excellent article explain this very thing at Coding Horror.