在 PHP 中,函数可以通过以下几种方式来传递参数:
function greetings($name, $message) {
echo "Hello, $name! $message";
}
greetings("Alice", "How are you?");
function greetings($name, $message) {
echo "Hello, $name! $message";
}
greetings(message: "How are you?", name: "Alice");
function greetings($name = "Alice", $message = "How are you?") {
echo "Hello, $name! $message";
}
greetings();
...
运算符,函数可以接受任意数量的参数。function sum(...$numbers) {
$total = 0;
foreach ($numbers as $number) {
$total += $number;
}
echo "The sum is: $total";
}
sum(1, 2, 3, 4, 5);