【问题标题】:Call to undefined function str_contains() php调用未定义的函数 str_contains() php
【发布时间】:2021-06-05 16:56:10
【问题描述】:

我的代码通过 APACHE 在 localhost 上运行,但是当我托管一个包含所有相同文件和代码的网站时,我收到以下消息: 致命错误:在 /storage/ssd3/524/16325524/public_html/static/header.php55行调用未定义函数 str_contains() >
显示错误的 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>';
                                }
                            }
                            ?>

【问题讨论】:

  • @BeshambherChaukhwan — 虽然我看不出 original issue 与此处的相关性,但请链接到该网站,而不是那个垃圾网站。
  • 明白????????

标签: php function compiler-errors


【解决方案1】:

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);
    }
}

【讨论】:

    【解决方案2】:

    str_contains() 是在 PHP 8 中引入的,在较低版本中不支持。

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

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

    【讨论】:

      【解决方案3】:

      The docsstr_contains 是在 PHP 8.0 中引入的。

      【讨论】:

        猜你喜欢
        • 2015-11-18
        • 2013-05-19
        • 2012-04-05
        • 1970-01-01
        • 1970-01-01
        • 2015-09-21
        • 2017-07-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多