We looked at the overall function declaration in the last article. The only new concept should be the parameters that are declared between the parentheses. You may have noticed that earlier I was talking about arguments and now I’m talking about parameters. When we call the function, we pass arguments; when we declare a function, we declare parameters. Quite a few programmers get that confused.
Functions can have multiple parameters. Each parameter is separated by a comma. The parameter naming convention is identical to the variable naming convention.
When passing arguments to the function, just envision that you’re assigning values to the parameters.
function_name("Subscribe", "Please");
Let’s modify the plant() function so that it can accept an argument and echo it out. We’ll declare a new function called plant(). That function will have one parameter $type_of_plant. The function will echo out whatever is assigned to the $type_of_plant parameter. The plant($type_of_plant) function will be called twice. Each time, a new argument will be supplied.
<?php
function plant($type_of_plant) {
echo "I'm an " . $type_of_plant . " plant<br>";
}
plant("Automotive");
plant("Vegetable");
?>
Let’s look at the code above in more detail.
- PHP skips over the function declaration.
- PHP encounters the plant(“Automotive”) function call on line 7.
- PHP finds the plant() function that’s being invoked and assigns the value “Automotive” to the $type_of_plant parameter on line 3.
- PHP enters the the function body and echoes “I’m an Automotive plant.”
- PHP reaches the end of the function and returns to line 7. The statement terminates with the semi-colon.
- PHP moves to the next line and encounters the next function call, plant(“Vegetable”).
- PHP finds the plant() function that’s being invoked and assigns the value “Vegetable” to the $type_of_plant parameter on line 3.
- PHP enters the function body and echoes “I’m an Vegetable plant.”
- PHP reaches the end of the function body and returns to line 8. The statement terminates with the semi-colon.
What if no argument is passed to the function plant()? Since it expects an argument, an error will be thrown. A default value can be assigned to the $type_of_plant parameter during function declaration. If plant() is invoked without an argument, the default value would be displayed. If plant() is invoked with an argument, the argument would override the default value, and the argument would be displayed. Assigning a default value to a parameter is syntactically identical to assigning a value to a variable ($type_of_plant = “Automotive”).
<?php
function plant_two( $type_of_plant = "Automotive" ) {
echo "I'm a(n) " . $type_of_plant . " plant<br>";
}
plant_two();
plant_two("Subaru");
?>
In the example above, when plant_two() is invoked without an argument, “I’m a(n) Automotive plan” is echoed out. When plant_two(“Subaru”) is invoked with an argument, “I’m a(n) Subaru plant” is displayed.
A function can be declared with some parameters containing default values and others without. The default-value-parameters will have to be pushed to the end of the declaration. Your required parameters will have to appear in the beginning. When working with multiple parameters, you have to provide arguments in the order that the parameters were declared. If the default parameters were in the beginning, and you skipped over them, how would PHP know what you skipped over? It just sees values coming in. There are programming languages where you can explicitly assign arguments to parameters when invoking functions, but PHP is not one of those languages. As a matter of fact, most programming languages follow the same rules as PHP does.
function func_name($p_1, $p_2, $p_3 = "Default") { }
Let’s look at an example where we mix both types of parameters. We’re going to create a function called car(). That function will be able to accept three arguments: the vehicle brand, the vehicle model, and the vehicle transmission. The $brand and $model parameters will be required. The $transmission parameter is optional and will default to “Manual.” The function body will echo out “You just a bought a BRAND MODEL in a(n) TRANSMISSION transmission.”
?php
function car( $brand, $model, $transmission = "Manual" ) {
echo "You just bought a " . $brand . " " . $model . " in a(n) " .
$transmission . " transmission<br>";
}
car( "Subaru", "WRX STi" );
car( "Jeep", "Wrangler", "Automatic" );
?>
During the first car(“Subaru”, “WRX STi”) function call, the $transmission argument is omitted. The $transmission parameter is assigned the default value “Manual.” As a side note, the STi only comes in a Manual transmission. Good job Subaru! The string “You just bought a Subaru WRX STi in a(n) Manual transmission” is echoed out.
The Jeep Wrangler comes in both options and the person that’s buying it just happens to want an automatic transmission. The car(“Jeep”, “Wrangler”, “Automatic”) function is called and all three arguments are specified. PHP outputs “You just bought a Jeep Wrangler in a(n) Automatic transmission.”
Function arguments are an integral part of programming. Make sure that you understand them.