Tracks
/
PHP
PHP
/
Syllabus
/
Variable-Length Arguments
Va

Variable-Length Arguments in PHP

1 exercise

About Variable-Length Arguments

Function arguments can be specified such that it can take any number of arguments:

<?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]

If specifying a variable-length argument in addition to others, it must be the last argument.

<?php

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

writeLines('file.txt', "a", "b", "c");
Edit via GitHub The link opens in a new window or tab

Learn Variable-Length Arguments