intdiv


php128 apg

RETURNS the integer quotient of the division between two integer values.





This function returns the integer quociente of division of $num1 by $num2.



<?php

int intdiv 
( int $num1 , int $num2 )


where,

$num1 = The dividend
            
$num2 
= The divisor
            
?>

$num1


The dividend.



$num2


The divisor.





If $num2 = 0 an exception is issued by DivisionByZeroError.



<?php

 DivisionByZeroError 
extends ArithmeticError {
/* Inherited properties */
protected string $message ;
protected 
int $code ;
protected 
string $file ;
protected 
int $line ;
/* Inherited methods */
final public Error::getMessage ( void ) : string
final public Error::getPrevious ( void ) : Throwable
final public Error::getCode ( void ) : mixed
final public Error::getFile ( void ) : string
final public Error::getLine ( void ) : int
final public Error::getTrace ( void ) : array
final public 
Error::getTraceAsString ( void ) : string
public Error::__toString ( void ) : string
final private Error::__clone ( void ) : void
}

?>



If $num1 = PHP_INT_MIN and $num2 = -1 an error is issued by ArithmeticError exception:



<?php

 ArithmeticError 
extends Error {
/* Inherited properties */
protected string $message ;
protected 
int $code ;
protected 
string $file ;
protected 
int $line ;
/* Inherited methods */
final public Error::getMessage ( void ) : string
final public Error::getPrevious ( void ) : Throwable
final public Error::getCode ( void ) : mixed
final public Error::getFile ( void ) : string
final public Error::getLine ( void ) : int
final public Error::getTrace ( void ) : array
final public 
Error::getTraceAsString ( void ) : string
public Error::__toString ( void ) : string
final private Error::__clone ( void ) : void
}

?>

  1 EXERCISE   

<?php

// Run this code several times

setlocale(LC_ALL, 'es-ES', 'es_ES');
 
$divd01a = mt_rand(30, 42);

$divs01a =  mt_rand(1, 30);

$quoc01a = intdiv($divd01a, $divs01a);

echo 
$divd01a . ' divided by ' . $divs01a . 
               
' produces quociente ' . $quoc01a . '<br><br>';

$quoc01b = $divd01a / $divs01a;

echo 
'The same integer part of ( ' . $divd01a . '/' 
                      
. $divs01a . ' ) = ' . $quoc01b . '<br>';

?> 

  2 EXERCISE   

<?php
 
$divd02a 
= PHP_INT_MAX;

$divs02a =  PHP_INT_MIN;

$quoc02a = intdiv($divd02a, $divs02a);

echo 
$divd02a . ' divided by ' . $divs02a . 
                        
' produces quociente ' . $quoc02a . '<br><br>';

?> 

  3 EXERCISE   

<?php

/* - - - - - - - - - - - - - - - - - - - - - - - - -
Careful division by zero.
 
This produces Fatal Error, 
interrupting the flow of the scripts. 
- - - - - - - - - - - - - - - - - - - - - - - - - - - */
 
$divd03a = PHP_INT_MAX;

$divs03a =  0;

$quoc03a = intdiv($divd03a, $divs03a);

echo 
$divd03a . ' divided by ' . $divs03a . 
                         
' produces quociente ' . $quoc03a . '<br><br>';

?> 

  4 EXERCISE   

<?php

/* - - - - - - - - - - - - - - - - - - - - - - - - -
Careful division
 
Produces Fatal Error, 
interrupting the flow of the scripts. 
- - - - - - - - - - - - - - - - - - - - - - - - - - - */
 
$divd04a = PHP_INT_MIN;

$divs04a =  -1;

$quoc04a = intdiv($divd04a, $divs04a);

echo 
$divd04a . ' divided by ' . $divs04a . 
                
' produces quociente ' . $quoc04a . '<br><br>';

?> 

  5 EXERCISE   

<?php

/*
 * If one of the values entered is FLOAT, 
 * or both, only the entire part will be taken 
 * into account, so the values will be 
 * considered as INTEGER.
 */
 
$divd05a = 22.999999;

$divs05a =  11.0123467;

$quoc05a = intdiv($divd05a, $divs05a);

echo 
$divd05a . ' divided by ' . $divs05a . 
                  
' produces quociente ' . $quoc05a . '<br><br>';

?>