Default argument values are used when a caller omits an optional parameter.

default argument A default argument gives a parameter a fallback value.

Default Arguments

subtotal
default_arguments.php
Replay: real traced execution (multi-file project)
<?php
function totalWithFee(int $subtotal, int $fee = 4): int {
    return $subtotal + $fee;
}

$subtotal = 20;
$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 = 8;
$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 = 50;
$standard = totalWithFee($subtotal);
$express = totalWithFee($subtotal, 10);

echo "subtotal=" . $subtotal . "\n";
echo "standard=" . $standard . "\n";
echo "express=" . $express . "\n";
  1. $subtotal ← 20

    6$subtotal→ 20 = 20; //@subtotal=8, 507$standard = totalWithFee($subtotal20);8$express = totalWithFee($subtotal, 10);
  2. function totalWithFee(int $subtotal, int $fee = 4): int

    pass 1 of 2
    1<?php2function totalWithFee(int $subtotal20, int $fee4 = 4): int {3    return $subtotal20 + $fee4;4}
  3. $standard ← 24

    6$subtotal = 20; //@subtotal=8, 507$standard→ 24 = totalWithFee($subtotal20);8$express = totalWithFee($subtotal20, 10);
  4. function totalWithFee(int $subtotal, int $fee = 4): int

    pass 2 of 2
    1<?php2function totalWithFee(int $subtotal20, int $fee10 = 4): int {3    return $subtotal20 + $fee10;4}
  5. $express ← 30

    7$standard = totalWithFee($subtotal);8$express→ 30 = totalWithFee($subtotal20, 10);910echo "subtotal=" . $subtotal20 . "\n";11echo "standard=" . $standard24 . "\n";12echo "express=" . $express30 . "\n";
    outputsubtotal=20
    standard=24
    express=30
  1. $subtotal ← 8

    6$subtotal→ 8 = 8;7$standard = totalWithFee($subtotal8);8$express = totalWithFee($subtotal, 10);
  2. function totalWithFee(int $subtotal, int $fee = 4): int

    pass 1 of 2
    1<?php2function totalWithFee(int $subtotal8, int $fee4 = 4): int {3    return $subtotal8 + $fee4;4}
  3. $standard ← 12

    6$subtotal = 8;7$standard→ 12 = totalWithFee($subtotal8);8$express = totalWithFee($subtotal8, 10);
  4. function totalWithFee(int $subtotal, int $fee = 4): int

    pass 2 of 2
    1<?php2function totalWithFee(int $subtotal8, int $fee10 = 4): int {3    return $subtotal8 + $fee10;4}
  5. $express ← 18

    7$standard = totalWithFee($subtotal);8$express→ 18 = totalWithFee($subtotal8, 10);910echo "subtotal=" . $subtotal8 . "\n";11echo "standard=" . $standard12 . "\n";12echo "express=" . $express18 . "\n";
    outputsubtotal=8
    standard=12
    express=18
  1. $subtotal ← 50

    6$subtotal→ 50 = 50;7$standard = totalWithFee($subtotal50);8$express = totalWithFee($subtotal, 10);
  2. function totalWithFee(int $subtotal, int $fee = 4): int

    pass 1 of 2
    1<?php2function totalWithFee(int $subtotal50, int $fee4 = 4): int {3    return $subtotal50 + $fee4;4}
  3. $standard ← 54

    6$subtotal = 50;7$standard→ 54 = totalWithFee($subtotal50);8$express = totalWithFee($subtotal50, 10);
  4. function totalWithFee(int $subtotal, int $fee = 4): int

    pass 2 of 2
    1<?php2function totalWithFee(int $subtotal50, int $fee10 = 4): int {3    return $subtotal50 + $fee10;4}
  5. $express ← 60

    7$standard = totalWithFee($subtotal);8$express→ 60 = totalWithFee($subtotal50, 10);910echo "subtotal=" . $subtotal50 . "\n";11echo "standard=" . $standard54 . "\n";12echo "express=" . $express60 . "\n";
    outputsubtotal=50
    standard=54
    express=60

Follow the Defaults

  1. subtotal starts as 20.
  2. totalWithFee($subtotal) uses the default fee 4.
  3. standard becomes 24.
  4. totalWithFee($subtotal, 10) uses the explicit fee 10.
  5. express becomes 30. | call | fee used | result | | --- | --- | --- | | totalWithFee(20) | 4 | 24 | | totalWithFee(20, 10) | 10 | 30 |

Exercise: default_arguments.php

Reproduce subtotal=20, standard=24, and express=30, then use the pinned subtotal variants 8 and 50 to predict standard=12 express=18 and standard=54 express=60.