Given that function calls often consume more resources than using a language constructor or the cast operator
This is false, at least conceptually speaking. A language construct can be quite complex. The only thing different about language construction is that the compiler has direct science to it.
In general a cast operator or just informs the compiler that he knows what he is doing, in which case it is of course faster than the function, or does a conversion operation that is essentially the same as calling the function. Okay, the call might be slightly worse. But it may be the opposite, have to see the actual implementation of each. It may be that cast does more things or is poorly done.
Differences
Of course there are situations where the function may be better or the only option. A second parameter has already been mentioned indicating conversion basis.
A cast can not convert anything to string properly. The strval()
function may be more successful, after all it tries to query the __toString()
method for do the conversion. cast does not do this.
Obviously some contexts only accept functions. This occurs in functions that expect you to send a string with a function name to do callback (awful thing to do).
Some people find the function more readable than cast . Not me.
Apparently implicit type coercion is done with casting .
Performance
For all I read it seems that cast in PHP is always faster, at least slightly. So in theory it should be used preferentially. A test that I found was in the OS . Another complete test .
I did a similar test in some environments and got differences close to 50% for better in cast using PHP 7 and passing 200% in PHP 5.x.
The test was done with an algorithm that does a string conversion to int
, it would not make sense to get an integer to convert to itself. I have run millions of times to measure the operation, not the preparation . The algorithm can be seen in PHP SandBox .
I saw Daniel Omine's test result. I did not see how it was done and results in 5.6. I saw that the result was 100 to 200 times slower than mine. Because it measures the interpretation of the code more than the operation itself.
That's what I always say, in many cases PHP will have spent significantly on the load and interpretation of the code and not so much on its execution itself. So performance in PHP is not so important, after all the mechanism is already very slow. In this example the preparation consumed at least 99% of the time.
I emphasize that I'm speculating a bit about the other test since it does not show how it was done. Then there may be deception about it.
Make your own test in the circumstances you are going to use and be aware of the changes in each version. What's worth today may not be worth tomorrow.
These tests tell me that the function is badly done and cast should be preferred, especially in PHP 5 where the difference is brutal.
Conclusion
That's what I've been looking for in the community, which I do not consider to be the most reliable. That may be so, but there may be something that no one has noticed. I will not delve into the sources of language to find out, I will trust. It does not seem to make a fundamental difference.