- Products -


Software


NEWS


Special



- Knowledge -


Category × Tag



- Like -

公開
作成日:2021/10/21
更新日:2022/3/4

【PHP】関数が存在するかを確認する方法

・str_contains関数を使っていたが、これはPHP8から利用可能な関数で、PHP7の環境では動かないということがあった。

・コードを書き換えるのは面倒なので、str_contains関数が無い場合は、関数を定義するという対応を行った。

・ここで使用したのが、function_exists関数である。

使い方


・str_contains関数が無かったら作成するコード

if(!function_exists("str_contains")){
    function str_contains($target, $check){
        if (strpos($target, $check) !== false) {
            return  true;
        }else{
            return  false;
        }
    }
}


参考


https://www.php.net/manual/ja/function.function-exists.php

https://newbedev.com/php-str-contains-php-7-code-example


Category



Tag




関連記事


{{tmp.name}}

{{article.category}} {{article.title}}