在 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);