Re: [RFC] Warning for implicit float to int conversions

From: Date: Thu, 04 Feb 2021 19:06:40 +0000
Subject: Re: [RFC] Warning for implicit float to int conversions
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-113081@lists.php.net to get a copy of this message
On 04/02/2021 17:54, Benjamin Morel wrote:
$age = $_GET['age']; // '25.75' $x = (int) $foo; // I'd expect a warning, not a silent conversion to 25
If we want to make explicit casts "fussy", that would be a much bigger change. Right now, the following are all valid, with no warnings, even though they are all lossy conversions:
    (int)25.75,
    (int)"25.75",
    (int)"hello",
    (int)"99 red balloons",
    (bool)2,
    (bool)"yes",
    (float)"3.1415 is not pi",
    (string)M_PI, // calling this "lossy" is a bit of a stretch, but it will retain decimal rather than binary precision
There's a case to be made for "strict casts" - I've pointed out before that so-called "strict types" mode encourages users to write code that is actually *less* strict, because explicit casts are very forgiving. However, they should probably be a new syntax, because there will be an ENORMOUS amount of code using deliberately using the existing casts in lossy situations. Regards, -- Rowan Tommins [IMSoP]

« previous php.internals (#113081) next »