PHP の 名前付き引数

レベル 5268 打鍵

名前付き引数を使って任意の順序でパラメータを渡します。

PHP のコード

// $ageと$activeは既定値があるので省略できる
1function createUser(
2 string $name,
3 string $email,
4 int $age = 0,
5 bool $active = true
6): string {
// 真偽値を文字列に変える
7 $status = $active ? "active" : "inactive";
// 文字列内の変数展開で結果を組み立てる
8 return "$name ($email), Age: $age, Status: $status";
9}
// 名前付き引数(PHP 8.0以降)は任意の順序で渡せる
// John (john@example.com), Age: 0, Status: inactiveと表示する
10echo createUser(
11 email: "john@example.com",
12 name: "John",
// $ageは省略され、既定値の0のまま
13 active: false
14);

実行結果

John (john@example.com), Age: 0, Status: inactive
このコードでタイピング練習する

このコードでタイピング練習ができます。記録はランキングに反映されません。

このコードで使う記号

この PHP のコードで打つ記号と、出てくる回数です。

  • "× 10
  • $× 10
  • ,× 7
  • :× 7
  • ;× 3
  • (× 3
  • )× 3
  • =× 3
  • ?× 1
  • .× 1

PHP のレベル 5 のほかのコード例

← PHP タイピング練習のトップへ