Open In App
Related Articles

How to create default function parameter in PHP?

Improve Article
Save Article
Like Article

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


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: 


    function greeting($name="GeeksforGeeks")
        echo "Welcome to $name "; 
    // Passing no value
    greeting("A Computer Science Portal"); 


Welcome to Gfg 
Welcome to GeeksforGeeks 
Welcome to A Computer Science Portal 

Example 2: 


    function welcome($first="GeeksforGeeks",
                     $last="A Computer Science Portal for Geeks")
                        echo "Greeting: $first $last";  


Greeting: GeeksforGeeks A Computer Science Portal for Geeks
Greeting: night_fury A Computer Science Portal for Geeks
Greeting: night_fury Contributor

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 28 Apr, 2023
Like Article
Save Article
Similar Reads
Complete Tutorials