PHP 8.6 | Make "characters to be trimmed" explicit - #1083
Open
jrfnl wants to merge 2 commits into
Open
Conversation
jrfnl
force-pushed
the
feature/php-8.6-trim-compatibility
branch
from
September 2, 2026 09:42
48fda07 to
0f27730
Compare
By default, the PHP native `[lr]trim()` functions, trim ASCII whitespace and the NUL byte character. PHP 8.6 changes the default value of the `$characters` parameter to also include the form feed - `"\f"` - character, which was previously not trimmed.⚠️ _Keep in mind that `[lr]trim()` only operates on the leading and/or trailing characters for a text string. It does not affect the characters in the "middle" !_ Requests uses `[lr]trim()` in various places throughout the codebase. To make this code PHP cross-version compatible, this commit makes the following changes: * Introduces a new `final` `Trim` class containing two class constants to represent the different PHP native default values for the `$characters` parameter. This class has been explicitly marked as not part of the public API to allow for changing that class to an `enum` once support for PHP < 8.1 has been dropped. * Makes the `$characters` being trimmed explicit in each of the `[lr]trim()` function calls (if it wasn't already). To determine which characters should be trimmed, the following rule of thumb has been used: _"Use the PHP 8.6 default (`Trim::WHITESPACE_CHARS`), except when the trimming may be subject to an RFC or other documented rules, in which case use the PHP < 8.6 default (`Trim::WHITESPACE_CHARS_NO_FF`)"_ That way, we preserve existing behaviour in "important" places, while benefitting from the new default value everywhere else. _Note: this PR does not update the tests. Tests should be updated to safeguard _changed_ behaviour, but the changes in this PR do not constitute significantly changed behaviour for those places where the behaviour was "changed" (where `Trim::WHITESPACE_CHARS` was used). And where the change _could_ be significant, the behaviour was not changed (`Trim::WHITESPACE_CHARS_NO_FF`)._ As a follow-up to this PR, an issue should be opened to review the trimming for those function calls where the behaviour was **_not_** changed in this PR (`Trim::WHITESPACE_CHARS_NO_FF`), against the applicable RFCs or other documentation. Refs: * https://wiki.php.net/rfc/trim_form_feed * https://www.php.net/manual/en/function.trim.php
The script to convert certain markdown documents to documents suitable for use in the GH Pages website, makes some calls to `trim()` functions. In all cases, form feed characters should be stripped (like PHP 8.6 will do by default). This commit standardized on the PHP 8.6 behaviour. Note: this commit does not use the new `Trim` class as Requests isn't loaded when running this script (stand-alone). Refs: * https://wiki.php.net/rfc/trim_form_feed * https://www.php.net/manual/en/function.trim.php
jrfnl
force-pushed
the
feature/php-8.6-trim-compatibility
branch
from
September 2, 2026 09:46
0f27730 to
6d9e419
Compare
schlessera
approved these changes
Sep 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PHP 8.6 | Make "characters to be trimmed" explicit
By default, the PHP native
[lr]trim()functions, trim ASCII whitespace and the NUL byte character.PHP 8.6 changes the default value of the
$charactersparameter to also include the form feed -"\f"- character, which was previously not trimmed.[lr]trim()only operates on the leading and/or trailing characters for a text string. It does not affect the characters in the "middle" !Requests uses
[lr]trim()in various places throughout the codebase.To make this code PHP cross-version compatible, this commit makes the following changes:
finalTrimclass containing two class constants to represent the different PHP native default values for the$charactersparameter.This class has been explicitly marked as not part of the public API to allow for changing that class to an
enumonce support for PHP < 8.1 has been dropped.$charactersbeing trimmed explicit in each of the[lr]trim()function calls (if it wasn't already).To determine which characters should be trimmed, the following rule of thumb has been used:
"Use the PHP 8.6 default (
Trim::WHITESPACE_CHARS), except when the trimming may be subject to an RFC or other documented rules, in which case use the PHP < 8.6 default (Trim::WHITESPACE_CHARS_NO_FF)"That way, we preserve existing behaviour in "important" places, while benefitting from the new default value everywhere else.
Note: this PR does not update the tests. Tests should be updated to safeguard changed behaviour, but the changes in this PR do not constitute significantly changed behaviour for those places where the behaviour was "changed" (where
Trim::WHITESPACE_CHARSwas used).And where the change could be significant, the behaviour was not changed (
Trim::WHITESPACE_CHARS_NO_FF).As a follow-up to this PR, an issue should be opened to review the trimming for those function calls where the behaviour was not changed in this PR (
Trim::WHITESPACE_CHARS_NO_FF), against the applicable RFCs or other documentation.Refs:
PHP 8.6 | GH Pages: make "characters to be trimmed" explicit
The script to convert certain markdown documents to documents suitable for use in the GH Pages website, makes some calls to
trim()functions.In all cases, form feed characters should be stripped (like PHP 8.6 will do by default).
This commit standardized on the PHP 8.6 behaviour.
Note: this commit does not use the new
Trimclass as Requests isn't loaded when running this script (stand-alone).Refs: