php function default value – Setting Default Values for Function Parameters

php function default value – PHP supports passing arguments by value , passing by reference, and default argument values.

php function default value

PHP allows us to set default argument values for function parameters. Php: Set default value to function argument.

You can set default values to function parameters.

PHP Default Argument Values Function

Example 1

<!--?php <br ?--> function sayHowDoYouDo($name="Ram"){
echo "HowDoYouDo $name
";
}
sayHowDoYouDo("Meera");
sayHowDoYouDo();//passing no value
sayHowDoYouDo("Vimal");
?>

Results

HowDoYouDo Meera
HowDoYouDo Ram
HowDoYouDo Vimal

Don’t Miss : How to call function in PHP from another file?

Read Also:  how to write data into csv file in javascript?

Example 2

<!--?php <br ?--> function invitationcard($first="Kohali",$last="Sidhupura"){
echo "Welcome: $first $last
";
}
invitationcard();
invitationcard("Karshan");
invitationcard("Dhoni","Mahendrshinh");
?>

Results

Welcome: Meera Sidhupura
Welcome: Karshan Sidhupura
Welcome: Dhoni Mahendrshinh

Example 3

<!--?php <br ?--> function add($n1=10,$n2=10){
$n3=$n1+$n2;
echo "Calculation is: $n3
";
}
add();
add(20);
add(40,40);
?>

Results

Calculation is: 20
Calculation is: 30
Calculation is: 80

I hope you get an idea about php function default value.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.