Skip to content
Learn Netverks

Lesson

Step 19/36 53% through track

parameters-return

Parameters, defaults, and return values

Last reviewed May 28, 2026 Content v20260528
Track mode
server_script
Means
Server runner
Reading
~1 min
Level
beginner

This lesson

This lesson teaches Parameters, defaults, and return values: the syntax, APIs, and habits you need before advancing in PHP.

Teams ship Parameters, defaults, and return values on every PHP codebase—skipping it leaves gaps in debugging and code reviews.

You will apply Parameters, defaults, and return values in contexts like: LAMP/LEMP stacks, Laravel apps, WordPress themes/plugins, and shared hosting.

Write PHP in the editor and click Run on server—the dev runner executes your script and returns stdout/stderr (set LEARNING_RUNNER_ENABLED=true locally).

When you can explain the previous lesson's ideas without copying starter code.

Functions accept required and optional parameters, can use variadics, and may return any type—including void or union types in PHP 8+.

Default values

function paginate(int $page = 1, int $perPage = 20): array {
    return ['page' => $page, 'perPage' => $perPage];
}

Defaults must be static expressions—no function calls unless constant.

Variadic ...$args

function sum(int ...$nums): int {
    return array_sum($nums);
}

Named arguments (PHP 8+)

paginate(perPage: 50, page: 2);

Nullable and union returns

function findUser(int $id): ?array { /* null if missing */ }

Important interview questions and answers

  1. Q: Pass by value vs reference?
    A: Scalars and arrays copy by default; prefix parameter with & to pass by reference—use sparingly.

Self-check

  1. Can required parameters follow optional ones?
  2. What does ?array mean as a return type?

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Default param order?
  • Variadic ... when?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump