PHP 가변길이 매개변수

1 개요[ | ]

variable length arguments in PHP
variable-length argument lists in PHP
PHP 가변길이 매개변수
PHP 가변길이 매개변수 함수

2 예시 1[ | ]

PHP 5.6+
function sum(...$numbers) {
    $acc = 0;
    foreach ($numbers as $n) $acc += $n;
    return $acc;
}
echo sum(1, 2, 3, 4);
# 10

3 예시 2[ | ]

PHP 5.6+
function test(...$args) {
	print_r( $args );
}
PHP 4+
function test() {
	print_r( func_get_args() );
}
실행결과 (공통)
test();
# Array
# (
# )
test('hello');
# Array
# (
#     [0] => hello
# )
test('hello', 'world');
# Array
# (
#     [0] => hello
#     [1] => world
# )

4 같이 보기[ | ]

5 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}