Hello Internals,
I've been planning this RFC for quite some time, and during the Thanksgiving break I finally had time to finish it:
https://wiki.php.net/rfc/deprecate-lossy-bool-conversions
Looking forward to your feedback,
Theodore Brown
Hello Internals,
I've been planning this RFC for quite some time, and during the Thanksgiving break I finally had time to finish it:
Hi Theodore,
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.
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.
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.
Thanks again for your work on the RFC.
--
Rowan Tommins
[IMSoP]
Quoted text · 4 lines
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.
Quoted text · 4 lines
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
Am 29.11.2026 um 15:33 schrieb Theodore Brown theodorejb@outlook.com: I've been planning this RFC for quite some time, and during the Thanksgiving break I finally had time to finish it: https://wiki.php.net/rfc/deprecate-lossy-bool-conversions
One thing I was surprised by when looking at the PR's changes https://github.com/php/php-src/pull/25889/changes is the huge number of tests which have been changed.
Looking through some of them I realized that they are sometimes cleanups (possibly triggered by the new deprecation warning?) but it is not always obvious why the change was necessary or if it maintains the spirit of the test ().
I am a bit worried about the actual impact on legacy code bases and will have to give the patch a try before coming to my final conclusion.
Regards,
- Chris
I am all for this, but I know a lot of people both on internals and externally who would rather defend PHP supporting bad code because of the amount of code out there which already is.
But anything which is hardening the language is a win in my book.
I do wonder though if when a function is type htined, it should be strict to that type. In your example of function setEnabled(bool $enabled): void {}
I would honestly expect '0' or '1' to throw a type error.