Call to undefined function str_contains() php
You are encountering a classic versioning conflict between PHP's core library and your deployment environment. The error message Call to undefined function str_contains() indicates that your server is running a version of PHP prior to 8.0, while your code relies on a feature introduced in that release.
The function str_contains() was officially added to the PHP core in version 8.0. It provides a case-sensitive check to determine if a substring exists within a string. Before this update, developers had to rely on regex patterns like strpos returning non-false values or native string comparison operators. When you deployed your files to a host running PHP 7.x (or an older version), the function simply does not exist in the global namespace, triggering a fatal error.
The source code provided demonstrates a common pattern for building navigation menus based on user sessions:
<?php
$menu = getAllData('meni');
global $korisnik;
for ($i = 0; $i < count($menu); $i++){
if(isset($_SESSION['korisnik'])){
if(str_contains($menu[$i]->naziv, 'login') || str_contains($menu[$i]->naziv, 'reg')){
continue;
}
echo '<li><a href='.$menu[$i]->putanja.'>'.$menu[$i]->naziv.'</a></li>';
}
else {
if (str_contains($menu[$i]->naziv, 'profile') || str_contains($menu[$i]->naziv, 'out')) {
continue;
}
echo '<li><a href=' . $menu[$i]->putanja . '>' . $menu[$i]->naziv . '</a></li>';
}
}
?>
To resolve this issue immediately without upgrading your server, you must polyfill the missing function. This involves defining str_contains locally within your application files or a dedicated compatibility file before any code attempts to call it. You can replicate the internal logic of the native function using strpos:
if (!function_exists('str_contains')) {
function str_contains($haystack, $needle) {
return $needle === '' || (strpos($haystack, $needle) !== false);
}
}
Place this definition at the very top of your entry point file (e.g., index.php) or in a bootstrap configuration file included before any logic runs. This ensures that when the rest of your application executes, the function is recognized regardless of the underlying PHP version.
However, relying on polyfills is often a temporary stopgap. The modern PHP ecosystem has moved significantly beyond version 7.x. Most shared hosting providers and cloud platforms now default to PHP 8.1 or 8.2 due to performance improvements in JIT compilation and memory management inherent in these newer versions. If your localhost environment (likely running PHP 8.0+) works perfectly but your production site fails, the discrepancy is almost certainly the server configuration.
You should verify the version of PHP running on your host by creating a simple info.php file containing <?php phpinfo(); ?> and accessing it via your browser. If it confirms a version lower than 8.0, you have two primary paths forward. The first is to request an upgrade from your hosting provider. Most modern hosts allow you to select the PHP version in the control panel. Switching to PHP 8.1 or higher removes the need for polyfills and grants access to other useful features like typed properties and improved error handling.
The second path involves upgrading your own codebase to be compatible with older environments, though this is rarely necessary unless you are targeting legacy enterprise systems. If you choose to stay on an older version, ensure that all dependencies in your composer.json specify a require constraint that supports PHP 7.x. For example:
"require": {
"php": "^7.4|^8.0",
"your-package": "^1.0"
}
If you are using a framework like Laravel, be aware that specific versions require specific PHP minimums. Laravel 9 requires PHP 8.1+, while Laravel 8 supports PHP 7.2+. Mismatching these requirements is a frequent cause of deployment failures.
In summary, the error is a version mismatch between your code's expectations and your server's reality. Fixing it involves either defining a fallback function to bridge the gap or, preferably, upgrading your server environment to match modern standards.
