In this lesson, you will learn about Default and Variable arguments in PHP Functions, their usage, and examples to better understand the topic.
PHP enables you to define the default value in a function declaration, and it will use the default argument value if you don’t pass any value to the function. Let’s look at a straightforward instance of using functional default arguments for PHP.
function DefaultArgumentFunc($FirstParameter=”SetValue”, $SecondParameter=1000){ // Code to be processed here } DefaultArgumentFunc ()
Example
<?php echo "PHP ***Default parameter*** Function Example"; echo "<br>"; echo " ------------Example (1)------------------ "; echo "<br>"; //concatenating string function defaultparamFunc($string2 = "John") { echo "Default parameter=", $string2, ' <br/>'; } defaultparamFunc("Sara"); defaultparamFunc(); // It will take default pass to func John defaultparamFunc("Smith"); echo "<br>"; echo " ------------Example (2)------------------ "; echo "<br>"; function incrementFunc($var1 = 500, $var2 = 1000) { echo "First value =", $var1, "\t", "Second value =", $var2; echo "<br/>"; } incrementFunc(); incrementFunc(400, 500); incrementFunc(1000, 99999); ?>
Output
PHP ***Default parameter*** Function Example ------------Example (1)------------------ Default parameter=Sara Default parameter=John Default parameter=Smith ------------Example (2)------------------ First value =500 Second value =1000 First value =400 Second value =500 First value =1000 Second value =99999
In the above example, when no value is passed to the function, it prints the default value set in the parenthesis of the function.
You can use the optional parameters to generate functions; you only need the parameter name with an assigned value.
Example
<?php echo "PHP ***Optional parameter*** Function Example"; echo "<br>"; echo " ------------Example (1)------------------ "; echo "<br>"; function customCars($carname = "Ferrari", $price = 100000) { echo "Name of car =", $carname, " and ", "Price of Car = ", $price, "<br/>"; } echo customCars("Audi,", 9999999); // print Name of car =Audi and Price of Car = 9999999 echo customCars("BMW"); //Name of car =BMW and Price of Car = 100000 echo customCars(8888888); //Name of car =8888888 and Price of Car = 100000 echo customCars(null, 8888888); //Name of car = and Price of Car = 8888888 echo customCars(); //Name of car =Ferrari and Price of Car = 100000 ?>
Output
PHP ***Optional parameter*** Function Example ------------Example (1)------------------ Name of car =Audi, and Price of Car = 9999999 Name of car =BMW and Price of Car = 100000 Name of car =8888888 and Price of Car = 100000 Name of car = and Price of Car = 8888888 Name of car =Ferrari and Price of Car = 100000
As you can see, the second customCars()
call omits the second argument, the car’s price. This causes the PHP engine to use the default value for the $price
parameter, which is 100000.
Now Suppose you want to pass the second parameter’s value. In that case, you must either make it null or pass an empty string, as in the fourth function call, or else the only argument present in the parenthesis will be treated as the first one, and its value will be printed, as in the third call. The last one causes the PHP engine to use the default value for the $price
parameter, which is 100000, is named ‘Ferrari’ because the first and second arguments are not passed.
Example
<?php echo "PHP ***Optional parameter*** Function Example"; echo "<br>"; echo " ------------Example (2)------------------ "; echo "<br>"; function customCars($carname, $price = 100000) { echo "Name of car =", $carname, " and ", "Price of Car = ", $price, "<br/>"; } echo customCars("Audi,", 9999999); // print Name of car =Audi and Price of Car = 9999999 echo customCars("BMW"); //Name of car =BMW and Price of Car = 100000 echo customCars(8888888); //Name of car =8888888 and Price of Car = 100000 echo customCars(null, 8888888); //Name of car = and Price of Car = 8888888 echo customCars(); //Fatal error: Uncaught ArgumentCountError: Too few arguments to function customCars(), 0 passed in on line 12 and at least 1 expected in Stack trace: #0 ?>
Output
PHP ***Optional parameter*** Function Example ------------Example (1)------------------ Name of car =Audi, and Price of Car = 9999999 Name of car =BMW and Price of Car = 100000 Name of car =8888888 and Price of Car = 100000 Name of car = and Price of Car = 8888888
Fatal error: Uncaught ArgumentCountError: Too few arguments to function customCars(), 0 passed on line and at least 1 expected in function
If you run the same example and only pass the second argument’s value, the outcome will be the same, and expect the third argument, assuming you provided a value when you defined the function. A fatal error will be thrown if you don’t pass at least one value when you call.
When generating default parameters, you should append function values to the end of your function’s prerequisite parameters. Writing a function in which the default parameters are not at the end of the function definition is syntactically valid will cause issues. Take the feature below.
Example
<?php echo "PHP ***Optional parameter*** Function Example"; echo "<br>"; echo " ------------Example ------------------ "; echo "<br>"; function customCars($carname, $price = 10000, $color, $model = 2017) { echo "Name of car =", $carname, "<br/>"; echo "Price of car =", $price, "<br/>"; echo "Color of car =", $color, "<br/>"; echo "Model of car =", $model, "<br/>"; echo "<br>"; echo "<br>"; } echo customCars("BMW", 5000000, "bule", 2018); //Name of car =BMW,Price of car =5000000,Color of car =bule,Model of car =2018 //echo customCars("Audi",,9999999); // Syntax Error echo customCars("BMW", null, "bule"); //Name of car =BMW,Price of car =,Color of car =bule,Model of car =2017 ?>
Output
PHP ***Optional parameter*** Function Example ------------Example ------------------ Name of car =BMW Price of car =5000000 Color of car =bule Model of car =2018 Name of car =BMW Price of car = Color of car =bule Model of car =2017
PHP offers functions with variable-length arguments. It indicates that a function can accept n, 1, or 0 arguments. To do so, you need to use 3 ellipses (dots) before the argument name. Since PHP 5.6, the three-dot concept has been used for variable-length arguments.
Let’s see a simple example of the PHP variable-length argument function.
Example
<?php echo "PHP *** Variable-Length Parameter*** Function Example"; echo "<br>"; echo " ------------Example (1)------------------ "; echo "<br>"; ?> function incrementFunc(...$numeric) { $increment = 0; foreach ($numeric as $n) { $increment += $n; } return $increment; } echo incrementFunc(10, 20, 30, 40); ?>
Output
PHP *** Variable-Length Parameter*** Function Example ------------Example (1)------------------ 100
This concludes the PHP Default and Variable Arguments lesson. In the next lesson, you will learn about Recursive and recursion functions in PHP.