Ошибка, связанная с str_ends_with
Есть оператор
while ($example=$query->fetch(PDO::FETCH_ASSOC))
{
$sizememorydatasql='SELECT name as name FROM goods WHERE id='.$example['goods_id'];
$sizememorydataquery=$pdo->prepare($sizememorydatasql);
$sizememorydataquery->execute();
$sizememorydataitem=$sizememorydataquery->fetch(PDO::FETCH_ASSOC)['name'];
$sizememorydata = explode(',',$sizememorydataitem)[4];
if (str_ends_with($sizememorydata, 'SSD')) {
$sqloutput[$i]['sizememorydata']=trim(substr($sizememorydata,0,strlen($sizememorydata)-strlen('SSD')),' ');
$sqloutput[$i]['memorytype']='SSD';
}
else {
$sqloutput[$i]['sizememorydata']=trim($sizememorydata,' ');
$sqloutput[$i]['memorytype']='HDD';
};
// $sqloutput[$i]['screendiagonal']=$example['screendiagonal_id'];
$i+=1;
// $return['outputtext'].='</div>';
};
$return['outputtext'].='</div>';
$sqlcountrecords = 'SELECT COUNT(*) as count FROM monoblocks';
$querycountrecords=$pdo->prepare($sqlcountrecords);
$querycountrecords->execute();
$return['countrecords']=$querycountrecords->fetch(PDO::FETCH_ASSOC)['count'];
//Return json encoded results
return json_encode([
'result' => $return,
'output' => $sqloutput
]
);
Выдает ошибку
Fatal error: Uncaught Error: Call to undefined function str_ends_with() in C:\OSPanel\domains\testsite.ru\stackoverflowsite\php\ajaxsubcategory1.php:127 Stack trace: #0 C:\OSPanel\domains\testsite.ru\stackoverflowsite\php\ajaxsubcategory1.php(162): ajaxValidate->formValidate() #1 {main} thrown in C:\OSPanel\domains\testsite.ru\stackoverflowsite\php\ajaxsubcategory1.php on line 127
Подскажите как устранить ошибку.
Ответы (1 шт):
Автор решения: MyZik
→ Ссылка
Проверьте Вашу версию PHP, эта функция доступна для PHP 8 и выше.
https://www.php.net/manual/ru/function.str-ends-with.php
Для PHP 7.4 можно использовать такую функцию:
/**
* @param string $haystack
* @param string $needle
*
* @return bool
*/
function endsWith(string $haystack, string $needle): bool
{
$count = strlen($needle);
if ($len === 0) {
return true;
}
return (substr($haystack, -$count) === $needle);
}
Для Вашей версии PHP 7.0 функция должна выглядеть так:
/**
* @param string $haystack
* @param string $needle
*
* @return bool
*/
function endsWith($haystack, $needle)
{
$count = strlen($needle);
if ($len === 0) {
return true;
}
return (substr($haystack, -$count) === $needle);
}
Использование:
$string = 'Hello World';
print_r(endsWith($string, 'World')); // true