The problem: A function that works fine on your main project suddenly throws "undefined function" the moment you run an older project on the same machine.
Environment: PHP 7.2/7.3/7.4 projects (commonly hit when running a legacy Laravel app like Laravel 7.x alongside newer PHP 8.x projects locally, e.g. in Laragon/XAMPP)
The Error
Fatal error: Uncaught Error: Call to undefined function str_starts_with()
Sometimes it's str_ends_with() or str_contains() instead — same root cause, same fix.
Why It Happens
str_starts_with(), str_ends_with(), and str_contains() are native PHP functions introduced in PHP 8.0. If your code (or a package your project depends on) uses one of these functions, but the project is actually running on PHP 7.x, the function simply doesn't exist yet in that PHP version — hence "undefined function."
This most commonly shows up in one specific scenario: you're running multiple Laravel projects locally with different PHP version requirements. A newer project (Laravel 9+, needing PHP 8+) works fine, but an older project — or a shared package used by both — was written assuming PHP 8's string functions are available, and breaks the moment it's forced onto PHP 7.x.
The Fix
You have two real options, depending on whether upgrading PHP is possible for that project:
Option A — Upgrade to PHP 8.0+ (best long-term fix): If the project itself doesn't have hard PHP 7-only dependencies, upgrading is the cleanest solution. Update your composer.json:
"require": {
"php": "^8.0"
}
Then run composer update and switch your local PHP version for that project (in Laragon: right-click tray icon → PHP → select version).
Option B — Polyfill the function (if you're stuck on PHP 7.x): If the project genuinely can't move to PHP 8 yet, define the missing function yourself, guarded so it doesn't conflict if PHP 8 is used later:
if (!function_exists('str_starts_with')) {
function str_starts_with(string $haystack, string $needle): bool {
return substr($haystack, 0, strlen($needle)) === $needle;
}
}
Put this in a file that's always loaded early — bootstrap/app.php in older Laravel, or a dedicated polyfills.php required from your composer.json autoload files. Or simpler: install the Symfony polyfill package, which does this for you:
composer require symfony/polyfill-php80
Step-by-Step
- Check which PHP version the failing project actually needs — open
composer.jsonand look at the"php"constraint. - Check which PHP version is currently active — run
php -vin that project's terminal. - If there's a mismatch and upgrading is possible, update
composer.json, switch the local PHP version, and runcomposer update. - If upgrading isn't possible (legacy client project, other hard PHP 7 dependencies), install
symfony/polyfill-php80instead — it's the safest option since it won't conflict with future upgrades. - Clear any cached autoload files:
composer dump-autoload. - Re-run the project — the function should now resolve normally.
If you're running several Laravel projects side-by-side on different PHP versions in Laragon, switching PHP globally to fix one project can silently break another that depends on the version you just switched away from. Set up per-site PHP versions (via Apache VirtualHost config) instead of relying on the single global PHP switch — this avoids the whack-a-mole problem entirely. Worth a dedicated post on its own.
Running into a different "undefined function" or "undefined method" error on a legacy project? Send it over on the Contact page.