Divide integer and get integer value
In PHP, you can divide two integers using the forward slash (/) operator.
In PHP, you can divide two integers using the forward slash (/) operator. To get the integer value of the division, you can use the intval() function. For example:
Example of dividing two integers using the forward slash (/) operator in PHP
<?php
$dividend = 10;
$divisor = 3;
$quotient = intval($dividend / $divisor);
echo $quotient; // Output: 3
<div class="alert alert-info flex not-prose">![]()
<span class="hidden md:block">Watch a video course</span>Learn object oriented PHP</div>
Alternatively, you can use the floor() function, which returns the largest integer less than or equal to the division result. Note that intval() truncates toward zero, while floor() rounds down, which affects how negative numbers are handled.
Example of dividing two numbers using the "floor" function in PHP
<?php
$dividend = 10;
$divisor = 3;
$quotient = floor($dividend / $divisor);
echo $quotient; // Output: 3For PHP 7 and later, the recommended approach is the intdiv() function, which performs integer division directly:
Example of dividing two integers using the intdiv() function in PHP
<?php
$dividend = 10;
$divisor = 3;
$quotient = intdiv($dividend, $divisor);
echo $quotient; // Output: 3You can also use the modulo (%) operator, which returns the remainder of a division rather than the quotient.
Example of dividing two numbers using the modulo operator % in PHP
<?php
$dividend = 10;
$divisor = 3;
$quotient = $dividend % $divisor;
echo $quotient; // Output: 1