DEV Community

Md Jannatul Nayem
Md Jannatul Nayem

Posted on

Default Parameters in PHP Functions

I hope you know what is a function and how to use a function. In short a function is a block of code which can take input and gives output but it is not necessary that a function will always take input and will always give output. It depends on the demand.
Now we will learn about default parameter of a function. Suppose you want to design a function where some parameters will receive value from arguments. But if arguments are not provided the function will use some default values for those parameters.

Here is an example:

<?php
function donate($account, $amount=2.00){
    echo "$amount dollar has transfered to account: $account\n";
}

donate("2014323432", 50.00);

Enter fullscreen mode Exit fullscreen mode

we are calling dontae function with two parameters. One is account number and amount of dollar. here 50.00 dollar has tranfered to 2014323432 account. But if you don't give amount, by default 2.00 dollar will be transfered to given account.

Note: after the default parameter, there won't be any required parameters. By position all the required parameters will come first and rest of others may be default parameters.
Because without this format php cannot map the arguments with parameters.

Top comments (0)