DEV Community

Discussion on: Descending Order

Collapse
 
aminnairi profile image
Amin

Worth noting that if you add the declare(strict_types=1) (since PHP 7.0.0) declaration at the top of your PHP script, integers wont coerce with floats and instead will throw an error as expected.

<?php

// https://repl.it/repls/OpenRowdyTrials

declare(strict_types=1);

function a(int $b) {
    return $b;
}

var_dump(a(1.1));
// PHP Fatal error:  Uncaught TypeError: Argument 1 passed to a() must be of the type integer, float given
Collapse
 
jamesrweb profile image
James Robb

Good to know, thanks! Interesting it isn't the default behaviour though but I assume this can be set in the .ini or elsewhere to be defaulted. I will look into that but either way, thanks for sharing!