Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer Default Parameters in Functions | Functions in PHP
PHP Core Concepts

bookDefault Parameters in Functions

Note
Definition

Default parameter values allow you to assign a predefined value to a function parameter so the function can be called even if the caller does not provide that argument.

When designing functions in PHP, you often want to make certain parameters optional. By assigning a default value to a parameter in a function definition, you allow the function to be called without explicitly providing that argument. If the caller omits the parameter, PHP automatically uses the default value. This feature makes your functions more flexible and easier to use, especially when there are common values that most callers will want.

index.php

index.php

copy
1234567891011121314
<?php // Function with a default parameter value function greet($name = "Guest") { echo "Hello, $name!"; } // Call the function without an argument (uses default) greet(); echo "\n"; // Call the function with an argument (overrides default) greet("Alice"); echo "\n"; ?>

Using default parameter values helps reduce repetitive code and prevents errors that might occur if a value is missing. You can specify default values for one or more parameters, but all parameters with default values must come after any required parameters in the function's parameter list.

question mark

What happens if you call a PHP function without providing a value for a parameter that has a default value?

Select the correct answer

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 1. Hoofdstuk 3

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

bookDefault Parameters in Functions

Veeg om het menu te tonen

Note
Definition

Default parameter values allow you to assign a predefined value to a function parameter so the function can be called even if the caller does not provide that argument.

When designing functions in PHP, you often want to make certain parameters optional. By assigning a default value to a parameter in a function definition, you allow the function to be called without explicitly providing that argument. If the caller omits the parameter, PHP automatically uses the default value. This feature makes your functions more flexible and easier to use, especially when there are common values that most callers will want.

index.php

index.php

copy
1234567891011121314
<?php // Function with a default parameter value function greet($name = "Guest") { echo "Hello, $name!"; } // Call the function without an argument (uses default) greet(); echo "\n"; // Call the function with an argument (overrides default) greet("Alice"); echo "\n"; ?>

Using default parameter values helps reduce repetitive code and prevents errors that might occur if a value is missing. You can specify default values for one or more parameters, but all parameters with default values must come after any required parameters in the function's parameter list.

question mark

What happens if you call a PHP function without providing a value for a parameter that has a default value?

Select the correct answer

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 1. Hoofdstuk 3
some-alt