0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

PHP 関数の引数 2種類のnullableな型(null許容型)の宣言を両方とも書く

Posted at

デフォルト値を設定するnull許容型の宣言

PHP7.0から使える デフォルト値にnullを指定する
引数にnullを渡してもエラーにならない

function showName(string $name = null) {
    var_dump($name);
}

型名の前に?を書くnull許容型の宣言

PHP7.1から使える 引数の型名の前に?を書く
デフォルト値が無いケースでは、エラーが発生する

function showName(?string $name) {
    var_dump($name);
}

null許容型の宣言を両方とも書く

PHP7.1から デフォルト値にnullを指定し、引数の型名の前に?を書く
両方とも記述して、デフォルト値が無いケースでも、エラーが発生しなくなる

function showName(?string $name = null) {
    var_dump($name);
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?