web関連
【PHP8】「Deprecated: nl2br(): Passing null to parameter」というエラー
php8に上げたら「Deprecated: nl2br(): Passing null to parameter #1 ($string) of type string is deprecated in …」というエラーが出たので備忘録
【原因】戻り値にnullを返していた
nl2br()を使い関数を出力していたのだが、その関数の戻り値でnullを返していたことで「Deprecated: nl2br(): Passing null to parameter #1 ($string) of type string is deprecated in …」とエラー文が出たよう。
なので、戻り値に「""(空文字)」を返してあげることで解決。
それ以前に、Warning: Undefined variableというエラー文も出ていたので、戻り値で返す変数の宣言を忘れていたことが原因っぽい。
おまけ:php8から型宣言が厳格化
php7系では型が曖昧でも良かった部分が、php8系からは厳格化したことで起きた現象。
WordPressでオリジナルのテーマを作っていたりすると、php8にした瞬間エラーが出る可能性あり。