Skip to content

[13.x] Optimize Str::ucfirst and Str::lcfirst using native PHP 8.4 functions#60864

Merged
taylorotwell merged 3 commits into
laravel:13.xfrom
bunyaminbilenkaratas:optimize/str-casing-methods
Jul 22, 2026
Merged

[13.x] Optimize Str::ucfirst and Str::lcfirst using native PHP 8.4 functions#60864
taylorotwell merged 3 commits into
laravel:13.xfrom
bunyaminbilenkaratas:optimize/str-casing-methods

Conversation

@bunyaminbilenkaratas

Copy link
Copy Markdown
Contributor

This PR optimizes the Str::ucfirst and Str::lcfirst methods by utilizing PHP 8.4's native mb_ucfirst and mb_lcfirst functions when they are available in the environment.

Why this matters:
Currently, these methods extract the first character using substr, mutate it (upper/lower), and concatenate it back with the rest of the string. Utilizing the native C-implemented multi-byte functions significantly reduces string segmenting overhead, avoids intermediate memory allocations, and provides a cleaner approach.

Backward Compatibility:
Backward compatibility is fully maintained for older PHP versions via a function_exists fallback.

All existing Str tests pass successfully.

Comment thread src/Illuminate/Support/Str.php Outdated
Comment thread src/Illuminate/Support/Str.php Outdated
bunyaminbilenkaratas and others added 2 commits July 22, 2026 22:39
Co-authored-by: Sebastian Hädrich <11225821+shaedrich@users.noreply.github.com>
Co-authored-by: Sebastian Hädrich <11225821+shaedrich@users.noreply.github.com>
@taylorotwell
taylorotwell merged commit da083a9 into laravel:13.x Jul 22, 2026
53 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants