軌道
/
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 編輯 連結會在新視窗或分頁中開啟

學習 可變長度引數