 
PHPでエラー「Call to undefined function str_starts_with()」が発生する原因と対処・修正案について紹介しています。
Fatal error: Uncaught Error: Call to undefined function str_starts_with() in ~
致命的なエラー:未定義関数 str_starts_with の呼び出し
確認環境
エックスサーバー
PHP 8.016
Call to undefined function str_starts_with() エラーの発生原因
「Call to undefined function str_starts_with()」というPHPエラーはPHPのバージョンがPHP8以前の状態でstr_starts_with()関数を呼び出した際に発生するエラーです。
このエラーは致命的なエラー(Fatal error)に分類されています。
例えば、PHP8より古いバージョンのPHPで以下のコードを実行しようとすると、同エラーが出力されます。
if (str_ends_with('Hallo World', 'World')) {
	echo 'true';
}else{
	echo 'false';
}
// trueエラーの解決策とstr_starts_with() の代替コード
「Call to undefined function str_ends_with() 」エラーを解決するには、PHPを8以上のバージョンに更新するのが一番良いです。
PHPのバージョンを上げる事ができない環境の場合は、例えば以下のようなコードで代用する事が可能です。
function endsWith($str, $judgement)
{
	$length = strlen($judgement);
	if ($length == 0 || (substr($str, -$length) === $judgement) != 1) {
		return 'true';
	} else {
		return 'false';
	}
}
print(endsWith('Hallo World', 'Hello'));
// true