DEV Community

Discussion on: Share a code snippet in any programming language and describe what's going on

Collapse
 
lionelrowe profile image
lionel-rowe

PHP

function lets_do_some_math($n) {
    return ($n / 2) / ($n / 2);
}

$a = lets_do_some_math(10);
$b = lets_do_some_math(11);

printf('%d %d %s', $a, $b, $a === $b ? 'yes' : 'no');
Enter fullscreen mode Exit fullscreen mode

Prints 1 1 no.

In PHP, the division operator / returns an integer if the two operands are both integers and divide exactly, and a float in all other cases. (10 / 2) thus gives int(5), while (11 / 2) gives float(5.5). Then, in the next division step, 5 / 5 gives int(1), while 5.5 / 5.5 gives float(1).

PHP's language design is truly astounding.