<?php
function register_template(){
print_r(func_get_args());
# the result was an array ( [0] => my template [1] => screenshot.png [2] => nice template .. )
}
register_template( # unkown number of arguments
$name = "my template",
$screenshot = "screenshot.png",
$description = "nice template .. "
)
?>
但是,我希望结果数组为 $key => $value 形式, $key 代表参数名称。
最佳答案
PHP 不支持任意数量的命名参数。您可以在函数声明中决定固定数量的参数及其名称,或者您只能获取值。
解决这个问题的通常方法是使用数组:
function register_template($args) {
// use $args
}
register_template(array('name' => 'my template', ...));
关于PHP 获取所有函数参数作为 $key => $value 数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4678382/