Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP intval vs floor

Is there any difference between floor() and intval()? Though both returns the same results, is there any issue with regard to performance? Which of the two is faster? Which is the right php function to use if I just want to return the whole value only of a decimal?

The goal here is to display 1999.99 to 1999, omitting the decimal value and only return the whole number.

$num = 1999.99;
$formattedNum = number_format($num)."<br>";
echo intval($num) . ' intval' . '<br />';
echo floor($num) . ' floor';
like image 852
basagabi Avatar asked Apr 28 '16 20:04

basagabi


People also ask

What does Intval mean in PHP?

Definition and Usage The intval() function returns the integer value of a variable.

What is PHP floor () function?

The floor() function is another in-built function in PHP iterpreter. This function accepts any float number as argument and rounds it down to the next lowest integer. This function always returns a float number as range of float is bigger than that of integer.

What is the difference between floor and round?

ROUND() function rounds the number up or down depends upon the second argument D and number itself(digit after D decimal places >=5 or not). FLOOR() function rounds the number, towards zero, always down.


1 Answers

The functions give different results with negative fractions.

echo floor(-0.1); // -1
echo intval(-0.1); // 0
like image 161
nl-x Avatar answered Sep 28 '22 16:09

nl-x