Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call to undefined function str_contains() php

My code works on localhost via APACHE, but when I hosted a website with all the same files and code i get this message: Fatal error: Call to undefined function str_contains() in /storage/ssd3/524/16325524/public_html/static/header.php on line 55
PHP Code where error shows:

<?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>';
                                }
                            }
                            ?>
like image 785
adam nikk Avatar asked Mar 07 '21 17:03

adam nikk


3 Answers

The manual page for str_contains says the function was introduced in PHP 8. I suspect your host is on PHP 7 (or possibly lower).

You can define a polyfill for earlier versions (adapted from 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);
    }
}
like image 166
rjdown Avatar answered Nov 04 '22 20:11

rjdown


str_contains() was introduced in PHP 8 and is not supported in lower versions.

For lower PHP versions, you can use a workaround with strpos():

if (strpos($haystack, $needle) !== false) {
  // haystack contains needle
}
like image 44
Petr Hejda Avatar answered Nov 04 '22 20:11

Petr Hejda


The docs say str_contains was introduced in PHP 8.0.

like image 7
Julia Avatar answered Nov 04 '22 21:11

Julia