Re: [RFC] Deprecate lossy conversions to and from bool

From: Date: Mon, 30 Nov 2026 19:29:36 +0000
Subject: Re: [RFC] Deprecate lossy conversions to and from bool
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-133265@lists.php.net to get a copy of this message
Hi Theodore, On 30/11/2026 03:30, Theodore Brown wrote:
I added a sentence to the Proposal clarifying that the deprecation will be promoted to a TypeError in the next major version, consistent with the existing deprecation of lossy float-to-int coercion. Hopefully that makes it sufficiently clear.
Thanks, makes sense to me.
I adjusted the wording in that paragraph to be more nuanced. It's true that 0 and 1 aren't the only boolean encodings, but they are the most widely used. With other encodings, the current implicit conversion is often wrong. E.g. the pgsql extension returns 't' and 'f' strings, and if 'f' is passed through a bool parameter or return type it is silently converted to true. The proposed deprecation will help avoid bugs like this.
Yes, that's a bit more balanced now. And to be clear, I was definitely not proposing that 'f' and 't' should be allowed, only surveying what string values are actually common.
The RFC already addresses HTML checkboxes briefly: since an unchecked checkbox doesn't submit any value, the correct check is isset() rather than converting the 'on' value.
Indeed, I mentioned it because the RFC mentions query strings, and query strings are often the result of HTML forms. So for many query strings, isset() or empty() are going to give better results than relying on implicit string conversions.
Even if '' were allowed to be passed to a bool parameter, "round-tripping" still wouldn't work without a deprecation. In your example, wants_string(false) would already emit a deprecation before the empty string reaches wants_bool(), since the RFC also deprecates false to string.
Ah, yes; I missed that that example breaks other rules in the RFC anyway. I don't think that invalidates my main point, though, which is that '' is the canonical string representation of false in PHP, so not accepting it seems awkward.
The reason '' isn't accepted is that it usually indicates a missing value rather than false, so allowing it would hide mistakes.
This feels like it's heading towards the "new set of boolean rules" which the RFC says it's trying to avoid. It's a logical argument, but it's not the rule that PHP uses anywhere else. The general rule in PHP is that "empty" values cast to false: 0, '', [], etc. The string '0' is actually the awkward special case. The more I think about it, the more I'm leaning towards forbidding *all* strings from boolean parameters, rather than trying to come up with a list of "good" values. Regards, -- Rowan Tommins [IMSoP]

« previous php.internals (#133265) next »