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

From: Date: Mon, 30 Nov 2026 03:30:52 +0000
Subject: Re: [RFC] Deprecate lossy conversions to and from bool
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-133261@lists.php.net to get a copy of this message
On Sunday, Nov 29, 2026 at 11:34 Rowan Tommins wrote: > Thanks for a very well-written RFC; I particularly appreciate the survey > of previous proposals and discussion of how this proposal differs. > > I think I am broadly in favour, but have two comments: > > Firstly, I am not a fan of open-ended deprecations, and read most of the > RFC thinking this was one. There is only one mention of "... and promote > to a TypeError in the next major release (PHP 9.0)". > > I think I agree with that timeline, but it should probably be mentioned > elsewhere as well - e.g. in the Proposal section, and in the Voting > description - so that people are clear that they are voting for the > future removal, not just the initial deprecation. Hi Rowan, Thank you for your thoughtful feedback. 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. > Secondly, I'm not totally sold on the choice of accepted strings. In the > FAQ you say this: > >> "Are the 0/1 and "0"/"1" values arbitrary?" >> >> No. They are the canonical encoding of booleans used by database >> columns, query strings, and many APIs. > > This claim feels a bit strong to me. PostgreSQL canonically encodes > booleans as 'f' and 't'; a lot of other databases (e.g. MySQL, SQLite, > SQL Server, Oracle) don't have true booleans, and use integer 0 and 1 by > convention. HTML defaults the value of a selected checkbox to 'on' and > an unselected one is left undefined. 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. 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. > Perhaps more importantly, PHP itself produces '' not '0' when casting or > coercing a boolean false to string. Under the proposal, this code would > be rejected rather than printing bool(false): > > ``` > function wants_bool(bool $val) { >     var_dump($val); > } > function wants_string(string $val) { >      wants_bool($val); > } > wants_string(false); > ``` > > I think accepting the "round-trippable" values should be the minimum, so > 0 and '' for false, and 1 and '1' for true. I'd be happy accepting > '0' > as well, because it is already special-cased to false rather than > falling into the catch-all "non-empty string" case. 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. This is important to catch mistakes where a false error sentinel is passed to a string parameter or returned from a string function (see the renderJson and str_starts_with examples in the RFC introduction). In my experience, a function which takes an arbitrary string and passes it directly to a bool parameter is suspect. It should probably use an explicit comparison like $val !== '', or a (bool) cast if it really is intended to convert only '' and '0' to false and every other string to true.. The reason '' isn't accepted is that it usually indicates a missing value rather than false, so allowing it would hide mistakes. The RFC has an example where a phone number from an optional form field is accidentally passed to a bool parameter: if '' were accepted, the mistake would only be reported for users who entered a phone number. The analysis found real mistakes of this kind, such as ini_get_all("", "") in php-src's tests, and a Laravel test which passed UploadedFile constructor arguments to new File(), so '' became its bool $checkPath argument. In contrast, the only intentional round trip of false through '' in the Symfony and Laravel test suites was the ESI onerror flag in Symfony's HTTP cache. I added a paragraph to the RFC acknowledging this case and how such code can be updated. Kind regards, Theodore

« previous php.internals (#133261) next »