The default parameter concept comes from C++ style default argument values, same as in PHP you can provide default parameters so that when a parameter is not passed to the function. Then it is still available within the function with a pre-defined value. This function also can be called optional parameter.
Syntax:
function greeting($name=" parameter_value ")
Parameter: This function accepts a single parameter that is $name here, holds the parameter value.
Below examples illustrate the procedure to create and use the default function parameter:
Example 1:
<?php function greeting($name="GeeksforGeeks") { echo "Welcome to $name "; echo("\n"); } greeting("Gfg"); // Passing no value greeting(); greeting("A Computer Science Portal"); ?> |
Output:
Welcome to Gfg Welcome to GeeksforGeeks Welcome to A Computer Science Portal
Example 2:
<?php function welcome($first="GeeksforGeeks", $last="A Computer Science Portal for Geeks") { echo "Greeting: $first $last"; echo("\n"); } welcome(); welcome("night_fury"); welcome("night_fury","Contributer"); ?> |
Output:
Greeting: GeeksforGeeks A Computer Science Portal for Geeks Greeting: night_fury A Computer Science Portal for Geeks Greeting: night_fury Contributer
Recommended Posts:
- How to set a default parameter value for a JavaScript function ?
- Passing a function as a parameter in JavaScript
- How to pass an array as a function parameter in JavaScript ?
- JavaScript | Pass string parameter in onClick function
- How to get the javascript function parameter names/values dynamically?
- How to add a parameter to the URL in JavaScript ?
- HTML | DOM Parameter Object
- HTML | DOM Parameter value Property
- Argument vs Parameter in Java
- HTML | DOM Parameter name Property
- How to define a parameter for an object using HTML ?
- Fabric.js | Ellipse opacity Parameter
- JavaScript SyntaxError - Malformed formal parameter
- JavaScript SyntaxError - Redeclaration of formal parameter "x"
- PHP | Collator create() Function
- How to create an Asynchronous function in Javascript?
- How to create a function from a string in JavaScript ?
- How to Create Custom Errors using New Function in Golang?
- CSS | :default Selector
- C# | Default Constructor
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

