トラック
/
PHP
PHP
/
シラバス
/
可変長引数
可変

可変長引数 の PHP

1個の演習

可変長引数について

関数の引数は、いくつでも受け取れるように指定できます。

<?php

function actOnItems(...$items) {
  // $items is an array containing 0 or more values
  // used to call the function.
}

actOnItems(1, 2, 3, 4); // $items => [1, 2, 3, 4]

ほかの引数と一緒に可変長引数を指定する場合は、それを最後の引数にしなければなりません。

<?php

function writeLines($file_handle, ...$lines)
{
    put_file_contents($file_handle, implode("\n", $lines));
}

writeLines('file.txt', "a", "b", "c");
GitHubで編集 リンクは新しいウィンドウまたはタブで開きます

可変長引数を学習する