调用未定义函数 str_contains() php

Call to undefined function str_contains() php

我的代码通过 APACHE 在本地主机上运行,​​但是当我托管一个包含所有相同文件和代码的网站时,我收到此消息: 致命错误:调用 /storage/ssd3/524/16325524/public_html/static/header.php 中的未定义函数 str_contains() 55
PHP 错误显示的代码:

<?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>';
                                }
                            }
                            ?>

The docsstr_contains 是在 PHP 8.0 中引入的。

str_contains()在PHP8引入,低版本不支持

对于较低的 PHP 版本,您可以使用 strpos() 的解决方法:

if (strpos($haystack, $needle) !== false) {
  // haystack contains needle
}

manual page for str_contains 说该功能是在 PHP 8 中引入的。我怀疑您的主机在 PHP 7(或可能更低)上。

您可以为早期版本定义一个 polyfill(改编自 https://github.com/symfony/polyfill-php80

if (!function_exists('str_contains')) {
    function str_contains(string $haystack, string $needle): bool
    {
        return '' === $needle || false !== strpos($haystack, $needle);
    }
}

查看启用的 Apache 模块

sudo apachectl -M

禁用 7.4 模块

sudo a2dismod php7.*

启用php8

sudo a2enmod php8.1