트랙
/
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에서 편집 링크가 새 창이나 탭에서 열려요

가변 길이 인자 배우기