Copy or Share · Level 2 of 8

Functions get a copy

Pass an array to a function that adds an item. Watch the caller's array.

<?phpfunction addItem(array $list): array{    $list[] = 'pen';    echo 'inside: ', count($list), PHP_EOL;    return $list;}$cart = ['book'];addItem($cart);echo 'outside: ', count($cart), PHP_EOL;$cart = addItem($cart);echo 'after return: ', count($cart), PHP_EOL;

1. Predict the output

Tap the lines in the order you think they print.

  1. 1…
  2. 2…
  3. 3…
  4. 4…