|In this tutorial, you will learn how to use a default value for an argument variable in a PHP function. This default value is used by the function only if there is no value for the argument variable passed to the function in the function call. |
In the previous tutorials, you learned how to write a basic PHP function and you learned how to pass information to the function via the function call. Let's take a look at a simple function and function call.
Basic PHP Function and Function Call
As you can see, this Hello_Visitor function has one argument variable, $current_name. In the function call the value passed to the function for the $current_name variable is Nancy. But, as you can imagine, if there is no value passed in the function call, the only thing printed on the web page will be Welcome,. So, to prepare for situations when no value is passed to the function, you can set a default value to be used. Let's set the default value to everyone. Then, when the default value is used, the message sent to the web browser will be Welcome, everyone. Here is the altered code that will set a default value for the argument variable.
function function_name($argument = value)