Functions and Scope
Default Arguments
Default argument values are used when a caller omits an optional parameter.
Default Arguments
default_arguments.php
<?php
function totalWithFee(int $subtotal, int $fee = 4): int {
return $subtotal + $fee;
}
$subtotal = ;
$standard = totalWithFee($subtotal);
$express = totalWithFee($subtotal, 10);
echo "subtotal=" . $subtotal . "\n";
echo "standard=" . $standard . "\n";
echo "express=" . $express . "\n";
<?php
function totalWithFee(int $subtotal, int $fee = 4): int {
return $subtotal + $fee;
}
$subtotal = ;
$standard = totalWithFee($subtotal);
$express = totalWithFee($subtotal, 10);
echo "subtotal=" . $subtotal . "\n";
echo "standard=" . $standard . "\n";
echo "express=" . $express . "\n";
<?php
function totalWithFee(int $subtotal, int $fee = 4): int {
return $subtotal + $fee;
}
$subtotal = ;
$standard = totalWithFee($subtotal);
$express = totalWithFee($subtotal, 10);
echo "subtotal=" . $subtotal . "\n";
echo "standard=" . $standard . "\n";
echo "express=" . $express . "\n";
default argument
A default argument gives a parameter a fallback value.