PHP: add optional parameters to a function
<?php
// The optional parameters are given default values in the function declaration
function testOptionalParameters($first = 1, $second = 2, $third = 3) {
echo "$first, $second, $third" . PHP_EOL;
}
testOptionalParameters(); // Prints 1, 2, 3
testOptionalParameters(6); // Prints 6, 2, 3
testOptionalParameters(6, 7); // Prints 6, 7, 3
testOptionalParameters(6, 7, 8); // Prints 6, 7, 8
// Can't skip an optional parameter to get its default value; syntax error:
testOptionalParameters(, 7, 8);
|
|