Skip to content

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

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

theodorejb wants to merge 4 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) :)

@theodorejb
theodorejb force-pushed the deprecate-lossy-bool-conversions branch 2 times, most recently 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.

@alexdowad

Copy link
Copy Markdown
Contributor

No objection from me (on mbstring)

@theodorejb
theodorejb force-pushed the deprecate-lossy-bool-conversions branch from 885608a to 04b355a Compare November 29, 2026 13:49
@LamentXU123

LamentXU123 commented Nov 29, 2026 •

Copy link
Copy Markdown
Member

I agree with the deprecation RFC. And code-wise I think fine for phar and intl. Please go ahead 👍

@theodorejb
theodorejb force-pushed the deprecate-lossy-bool-conversions branch from 04b355a to 4625bd1 Compare November 29, 2026 14:31
@theodorejb

Copy link
Copy Markdown
Contributor Author

@Girgias Thanks! I checked zend_may_throw_ex(), and no changes are needed. For typed properties, the only assignments it treats as unable to throw are ones where the value already has the property's exact type, so no conversion happens and the deprecation can't be emitted. All other assignments to typed properties are treated as possibly throwing, so the deprecation is handled the same way as the existing float to int deprecation.

I also added ext/opcache/tests/jit/assign_obj_008.phpt to cover lossy conversions when assigning to typed properties under the JIT.

@TimWolla TimWolla added the RFC label Nov 29, 2026

@NickSdot NickSdot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for your work on this. I'll leave feedback to the RFC to people who write PHP in non-strict mode. I'd, however, like to offer feedback to the test changes.

  1. Not all test changes seem too obvious to me.
  2. Some useful test coverage was lost.
  3. Discussing all test changes here will become noisy.

My three comments are examples. Perhaps it would be good if the surfaced test mistakes, as well as the type variation test changes/removals, could be addressed in their own PRs (they should be addressed either way, no matter the RFC outcome). All this being surfaced by the RFC is a good argument for the RFC. Though, splitting PRs would not prevent using it as supporting argument in the RFC.

Many older tests include booleans in their lists of inputs for int, float, or string parameters, or other scalars in their lists of inputs for bool parameters. Each of these inputs is now replaced with the value it was converted to (0, 0.0, or "" for false, and 1, 1.0, or "1" for true), or removed when the test already has an input which converts to the same value. The functions are called with the same values as before, so the tests keep their coverage without relying on implicit conversions to or from bool. Booleans which are not converted, such as array elements joined by implode() or $res_arr values in tempnam_variation3-win32.phpt, are kept.

The fscanf() tests which write booleans to a file as test data now convert them to strings explicitly. call_user_func_strict_arginfo_check.phpt passes an int instead of false to strlen(), so it still covers a weak mode conversion. The get_cfg_var(), proc_nice(), and imageantialias() tests which only passed booleans are removed, as the values they converted to are covered by the other tests of these functions, with an empty string case added to get_cfg_var_variation7.phpt. The ReflectionClass::getConstant(), hasConstant(), hasMethod(), and hasProperty() tests of invalid arguments are also removed, as they only test parameter parsing.
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(), curl_exec(), mktime(), imageloadfont(), mb_ereg_replace(), and locale_get_display_name() failures returning false were passed on to other functions.
- The Windows tempnam() prefix variation test, which passed the false returned by tempnam() on to file_exists() and dirname(), also passed TRUE and FALSE as prefixes. TRUE is now passed as "1", and FALSE is removed, since the test already passes an empty string.
- The FPM log reader returned false from fgets() at the end of the stream from a ?string method, which now returns null instead.
- Three OpenSSL session resumption tests waited for a server message which the test helper had already printed, so the helper's ?string wait() returned false from fgets() at the end of the stream.
- Two memory checks looped until feof() and passed the false returned by fgets() at the end of /proc/meminfo to preg_match().
- A mysqli debug test passed the false returned for a trace file which is expected not to be written to explode().
- PDO_ODBC tests passed false from getenv() as the user and password when they were not set, instead of null.
- Arbitrary strings or ints were passed to the bool parameters of ini_get_all(), strstr(), substr_compare(), IntlRuleBasedBreakIterator::__construct(), and tidy_repair_file(), and assigned to the bool SNMP::$oid_increasing_check property.
- false was passed instead of an empty string to bind_textdomain_codeset() and locale_lookup().
- ob_start(), openssl_decrypt(), 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.
- SessionHandlerInterface::gc() implementations 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().
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
The deprecation is expected in tests of the coercion behavior itself, such as the scalar and union type declaration tests, the ZPP weak mode tests, exit() values, and the NAN coercion tests, in the IntlTimeZone::getCanonicalID() test which assigns its bool by-reference result to a typed string property, in the PDO_DBLIB test of quoting booleans, 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.
@theodorejb
theodorejb force-pushed the deprecate-lossy-bool-conversions branch from 4625bd1 to e8e0e67 Compare December 1, 2026 03:44
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.

6 participants