Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele 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

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 3

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

bookDefault Parameters in Functions

Pyyhkäise näyttääksesi valikon

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

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 1. Luku 3
some-alt