In languages like C or Python 2, if I divide an integer by an integer, I get an integer:
>>> 8/3
2
But in PHP, if I divide an integer by another integer with /, sometimes I get a float:
php > var_dump(6/3);
int(2)
php > var_dump(8/3);
float(2.6666666666667)
I’d like to do division like in Python or C, so that 8/3 is 2. How can I do that in PHP?
As of PHP 7, we can use the intdiv built-in function to get an integer value:
For earlier versions of PHP, we can use the
round()function to get an integer rounded value:Or we can use the
floor()function to get an integer value: