Skip to content

RFC: Deprecate lossy conversions to and from bool - #25889

Open
theodorejb wants to merge 3 commits into
php:masterfrom
theodorejb:deprecate-lossy-bool-conversions
Open

theodorejb wants to merge 3 commits into
php:masterfrom
theodorejb:deprecate-lossy-bool-conversions

Conversation

@theodorejb

Copy link
Copy Markdown
Contributor

@Girgias

Girgias commented Nov 29, 2026

Copy link
Copy Markdown
Member

I know @iluuu1994 told me the main reason he voted against my related RFC was because of the int -> bool so his input might be more valuable than mine if special casing 0/1 is sufficient. But overall in favour of the proposal (which shouldn't be a surprise) :)

In coercive typing mode, emit E_DEPRECATED when a parameter, return value, or typed property coerces a value to bool which has no canonical bool representation (an int other than 0 or 1, any float, or a string other than "0" or "1"), or coerces a bool to int, float, or string. This also applies when a union type without bool, such as int|float or int|string, selects another type for a bool.

NAN coerced to bool emits the deprecation in addition to the existing "unexpected NAN value was coerced to bool" warning.

zend_parse_arg_str_or_long_slow() no longer falls back to string when the deprecation for the int conversion is promoted to an exception.

The compiler and optimizer previously evaluated a constant $strict argument to in_array() with zend_is_true(), bypassing the parameter type check. This is now only done for true and false, so other values go through the regular call. As a side effect, in_array($needle, $haystack, 1) now throws a TypeError under strict_types, as with any other bool parameter.

RFC: https://wiki.php.net/rfc/deprecate-lossy-bool-conversions
@theodorejb
theodorejb force-pushed the deprecate-lossy-bool-conversions branch from a53b483 to bf065ce Compare November 29, 2026 04:33
The lossy bool conversion deprecation surfaced a number of mistakes in tests, which are fixed here instead of recording the deprecation in the expected output:

- A misplaced parenthesis passed the result of `substr(PHP_OS, 0, 3) == 'WIN'` to strtoupper() in the poll test helper and two stream tests. This only worked because true and false become "1" and "".
- Trampoline __call() methods used as predicates were declared as returning string instead of bool.
- Iterator::valid() implementations returned a filename string or an int counter.
- Reflection tests called strlen() on a false doc comment before checking for false.
- fread(), fgets(), inet_pton(), iconv_mime_decode(), file_get_contents(), tempnam(), and curl_exec() failures returning false were passed on to string parameters.
- Arbitrary strings or ints were passed to the bool parameters of ini_get_all(), strstr(), and substr_compare().
- ob_start() and debug_backtrace() were called with a bool for parameters which were bool before PHP 5.4 and 5.3.6, but are now int flags.
- array_filter() was called with true instead of ARRAY_FILTER_USE_BOTH.
- A SessionHandlerInterface::gc() implementation returned true instead of the number of deleted sessions.
- A get hook for an int property returned the bool result of isset().
- false was passed to an int variadic parameter.
- The bool returned by touch() was passed to date() as the timestamp.
- false was passed to PDOStatement::execute(), which binds every value as a string, so a user-defined SQLite function received an empty string for its bool parameter. It is now bound with PDO::PARAM_BOOL.
- run-tests.php printed the current directory as the loaded php.ini when no php.ini was loaded, as false was passed to realpath().
Tests which only exist to pass each type to one parameter of a function (such as the "different data types" variation tests and "invalid params" tests) are removed, as this type juggling is global behavior already covered by the Zend type declaration tests. Other tests which included booleans in their list of int, float, or string inputs, or other scalars in their list of bool inputs, no longer pass them, so they won't need updating again when the deprecation becomes a TypeError. The fscanf() tests which write booleans to a file as test data now convert them to strings explicitly.

The deprecations are recorded only in tests of the coercion behavior itself, such as the scalar and union type declaration tests, exit() values, and the NAN coercion tests, and in the regression test for bug #79919, which needs the bool returned by error_log() to reproduce the bug. The phpGH-22112 test now ignores the deprecation in its error handler, so it continues to cover an exception thrown by the NAN warning.

The 32-bit and non-Windows variants of affected tests were updated to match, but could not be run locally.
@theodorejb
theodorejb force-pushed the deprecate-lossy-bool-conversions branch from bf065ce to 381e820 Compare November 29, 2026 04:46

@Girgias Girgias left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might need to review zend_may_throw_ex() function for the ZEND_ASSIGN_OBJ opcode and any opcode that assigns to typed properties as they follow the same semantics as function param/return vals.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants