PHP is a dynamically typed language and as such implicit type coercion naturally arises, most of these are harmless and rather convenient.
However, the conversion from boolean to string is asymmetric, as false
gets coerced to an empty string ''
and true
to the string '1'
, and is a rare occurrence which most likely hints to a bug.
Emit an E_DEPRECATED
deprecation diagnostic for implicit coercion of a boolean to a string.
The diagnostic message is:
Implicit bool to string coercion is deprecated
Raise this deprecation diagnostic to a TypeError in the next major version (PHP 9.0).
Amending the type signature of the following functions as their usage with a boolean argument is common:
ini_set()
A code audit of the impact of this change was realized back in 2015 for the "Coercive Types for Function Arguments" RFC [1] and any occurrence of this happening was a symptom of a bug rather then desirable behaviour.
PHP's boolean conversion to string is also unique, databases will convert true
/false
into '1'
/'0'
, and other programming languages if they support such a conversion usually convert it to 'true'
/'false'
. Moreover, the fact that false
gets converted to an empty string ''
means that as of PHP 8.0, it does not produce a valid numeric string, resulting in a broken chain of implicit coercion if the string is used in a numeric context later on.
The following operations will now emit an E_DEPRECATED if a boolean is used:
.
echo
language constructprint
language constructstring
in coercive typing modestring
for both internal and userland functions in coercive typing modestring
in coercive typing modeDeprecation and function signature changes: next minor version, i.e. PHP 8.1.
Promotion to TypeError: next major version, i.e. PHP 9.0.
printf()
family of functionsfalse
being casted to stringAs per the voting RFC a yes/no vote with a 2/3 majority is needed for this proposal to be accepted.
Links to any external patches and tests go here.
If there is no patch, make it clear who will create a patch, or whether a volunteer to help with implementation is needed.
Make it clear if the patch is intended to be the final patch, or is just a prototype.
For changes affecting the core language, you should also provide a patch for the language specification.
After the project is implemented, this section should contain
[1]: PHP RFC: Coercive Types for Function Arguments https://wiki.php.net/rfc/coercive_sth#changes_to_internal_functions