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
Hi Theodore,
I added a sentence to the Proposal clarifying that the deprecation will be promoted to a
TypeErrorin 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
0and1aren'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 totrue. 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 reacheswants_bool(), since the RFC also deprecatesfalsetostring.
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]
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.
Hi Theodore,
I enjoyed reading this RFC because it was so much better than the recent deprecate-fuzzy-casts RFC.
My only critique is that this RFC must mention the php.ini treatment of bools, and whether or not that would be changed. The concern here: Very near the top of php.ini-production, we have a pattern of setting the bools display_startup_errors, log_errors, etc, using INI literals On and Off.
https://github.com/php/php-src/blob/82ec0c066ca6d6d60713e0c7aedc24af56f879d6/php.ini-production#L102
https://www.php.net/manual/en/errorfunc.configuration.php#ini.display-startup-errors
If this is not directly relevant to the RFC, that still should be clearly stated in the RFC, perhaps even with a brief discussion of the ini_get() behavior.
Thank you for your efforts!
Robert Chapin
Quoted text · 5 lines
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
Quoted text · 6 lines
I enjoyed reading this RFC because it was so much better than the recent deprecate-fuzzy-casts RFC.
My only critique is that this RFC must mention the php.ini treatment of bools, and whether or not that would be changed. The concern here: Very near the top of php.ini-production, we have a pattern of setting the bools display_startup_errors, log_errors, etc, using INI literals On and Off.
https://github.com/php/php-src/blob/82ec0c066ca6d6d60713e0c7aedc24af56f879d6/php.ini-production#L102
https://www.php.net/manual/en/errorfunc.configuration.php#ini.display-startup-errors
If this is not directly relevant to the RFC, that still should be clearly stated in the RFC, perhaps even with a brief discussion of the
ini_get()behavior.Thank you for your efforts!
Hi Robert,
Thank you for bringing this up.
The way PHP reads INI settings isn't affected by the RFC. The INI parser stores On/Off (as well as yes/no and true/false) as the strings '1' and '', and boolean settings like display_startup_errors and log_errors are read from those strings without going through a type declaration, so php.ini files using On and Off keep working exactly as before. ini_set() declares $value as string|int|float|bool|null, so passing a bool to it isn't deprecated either.
The one change is for code which passes ini_get() values directly to a bool parameter. ini_get() returns the stored string unchanged: '1' for a setting set to On or 1, '0' for 0, and '' for Off, so the '' would get a deprecation, as with any empty string. But ini_get() values already need explicit parsing: after ini_set('display_errors', 'Off'), ini_get() returns the literal 'Off', which (bool) turns into true, even though PHP itself treats the setting as disabled. Since 'Off' passed to a bool parameter would also emit a deprecation, the RFC helps catch places where boolean INI settings aren't properly parsed. filter_var($value, FILTER_VALIDATE_BOOL) handles all of these correctly.
I've added a note about this to the "Unaffected PHP functionality" section of the RFC.
Thanks,
Theodore