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?

アロー関数

Last updated at Posted at 2025-12-07

アロー関数

アロー関数は無名関数をより短く記述するための機能

  • 基本構造
    無名関数はuseが必要だがアロー関数では1行の式で書くことができて外部変数を自動で参照できる
// 無名関数 (useが必要)
$taxRate = 0.10;
$calcTax = function ($price) use ($taxRate) {
    return $price * $taxRate;
};

// アロー関数 (useが不要)
$taxRate = 0.10;
$calcTaxArrow = fn ($price) => $price * $taxRate;
  • 主な利用例
メソッド アロー関数で記述
map $collection->map(fn ($item) => strtoupper($item));
filter $collection->filter(fn ($item) => $item > 10);
groupby $collection->groupBy(fn ($user) => $user->company);
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?