PHP intval vs floor PHP intval vs floor php php

PHP intval vs floor


The functions give different results with negative fractions.

echo floor(-0.1); // -1echo intval(-0.1); // 0


Per the docs,

The return value of floor() is still of type float because the value range of float is usually bigger than that of integer.

Intval() returns an int, however.


The fastest way is to use type casting: (int)$num.