# [RFC] Deprecate boolean to string coercion

- Thread: https://externals.io/message/115048
- Original archive: https://news-web.php.net/php.internals/115048
- Started by G. P. B. on 2021-06-22T22:10:42+00:00
- Messages: 4
- RFC: Deprecate boolean to string coercion (Inactive) — https://externals.io/rfc/deprecate-boolean-string-coercion.md
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #115048 — G. P. B. — 2021-06-22T22:10:42+00:00

URL: https://externals.io/message/115048#115048

Hello Internals,

With Ilija we are proposing a short RFC to deprecate coercion from bool to
string:
https://wiki.php.net/rfc/deprecate-boolean-string-coercion

As this is the final day for any RFC to be even able to land in PHP 8.1
the voting is expected to start in two weeks on the 6th of July.

The implementation is yet to be done but is expected to be rather
straightforward and finished within the week.

Best regards,

George P. Banyard

---

## #115049 — tyson andre — 2021-06-22T22:52:09+00:00

URL: https://externals.io/message/115048#115049 · In reply to: #115048

Hi George P. Banyard,

> With Ilija we are proposing a short RFC to deprecate coercion from bool to
> string:
> https://wiki.php.net/rfc/deprecate-boolean-string-coercion
> 
> As this is the final day for any RFC to be even able to land in PHP 8.1
> the voting is expected to start in two weeks on the 6th of July.
> 
> The implementation is yet to be done but is expected to be rather
> straightforward and finished within the week.

I'd agree any casts from booleans to strings are usually a bug in the application

Something I'd like to see in the rfc: What's the intended behavior (notices) of `sprintf('%s', false);` (functions internally casting to strings)
What about `echo true; print(false);`, etc.

Thanks,
Tyson

---

## #115141 — G. P. B. — 2021-06-25T13:11:06+00:00

URL: https://externals.io/message/115048#115141 · In reply to: #115049

> Hi George P. Banyard,
>
> > […]
> to
> > […]
>
> I'd agree any casts from booleans to strings are usually a bug in the
> application
>
> Something I'd like to see in the rfc: What's the intended behavior
> (notices) of `sprintf('%s', false);` (functions internally casting to
> strings)
> What about `echo true; print(false);`, etc.
>
> Thanks,
> Tyson
> --
> 
> To unsubscribe, visit: https://www.php.net/unsub.php


Hello Tyson,

Thanks for pointing this out, we clarified this in the RFC now.

Moreover, seeing the impact on the tests of php-src we also propose to
change some function signatures to accept boolean arguments.
The only such function currently is `ini_set()`, but others might be added.

Best regards,

George P. Banyard

---

## #115162 — Kamil Tekiela — 2021-06-26T17:48:20+00:00

URL: https://externals.io/message/115048#115162 · In reply to: #115141

Hi George,

As someone who has fallen victim to this silent bug in the past, I think I
would be on board with this change.
Why only implicit casting? If we compare this to the array-to-string
casting then the warning happens for both. It would be more consistent to
deprecate both implicit and explicit. I'd imagine it would also be easier
to implement. I'd say that `printf()` family of functions should also throw
the deprecation notice, even if we only deprecate implicit conversion.
I am under impression that most of the time when this kind of conversion
happens the expected outcome is either 0 or 1. For example, when storing a
boolean flag to sqlite database. Could we not change the false value to 0
instead of deprecating?

Regards,
Kamil
