CodesCompiler
🔍
HTMLCSSJavaScriptPythonSQLPHPSEOPostsBooksKids About Contact 🚀 Get Started Free

PHP - Default Arguments

PHP - Default Arguments

Like most of the languages that support imperative programming, a function in PHP may have one or more arguments that have a default value. As a result, such a function may be called without passing any value to it. If there is no value meant to be passed, the function will take its default value for processing. If the function call does provide a value, the default value will be overridden.

Function Definition with Default Values

Here is the way you can define a function with default values −

PHP CODE
function fun($arg1 = val1, $arg2 = val2) {
   Statements;
}

Different Ways to Call the Function

The above function can be called in different ways −

PHP CODE
fun();			# Function will use defaults for both arguments
fun($x);		# Function passes $x to arg1 and uses default for arg2
fun($x, $y);	# Both arguments use the values passed

Example 1

Here we define a function calledgreeting()with two arguments, both havingstringas their default values. We call it by passing one string, two strings and without any argument.

PHP CODE
<?php  
   function  greeting($arg1="Hello", $arg2="world") {
      echo $arg1 . " ". $arg2 . PHP_EOL;
   }

   greeting();
   greeting("Thank you");
   greeting("Welcome", "back");
   greeting("PHP");
?>

Output

It will produce the following output −

PHP CODE
Hello world
Thank you world
Welcome back
PHP world

Example 2

You can define a function with only some of the arguments with default value, and the others to which the value must be passed.

PHP CODE
<?php  
   function  greeting($arg1, $arg2="World") {
      echo $arg1 . " ". $arg2 . PHP_EOL;
   }

   # greeting(); ## This will raise ArgumentCountError
   greeting("Thank you");
   greeting("Welcome", "back");
?>

Output

It will produce the below result −

PHP CODE
Thank you World
Welcome back

The first call (without argument) raisesArgumentCountErrorbecause you must pass value for the first argument. If only one value is passed, it will be used by the first argument in the list.

However, if you declare arguments withdefaultbefore arguments without defaults, such function can be only called if values for both are passed. You cannot have a situation where the first argument uses the default, and the second using the passed value.

Thegreeting()function now has$arg1with default and$arg2without any default value.

PHP CODE
function  greeting($arg1="Hello", $arg2) {
   echo $arg1 . " ". $arg2 . PHP_EOL;
}

If you pass a string “PHP” −

PHP CODE
greeting("PHP");

with the intension to print the result as “Hello PHP”, the following error message will be displayed.

PHP CODE
PHP Fatal error: Uncaught ArgumentCountError: Too few arguments to function 
greeting(), 1 passed in hello.php on line 10 and exactly 2 expected

Example 3

Let’s define a functionpercent()that calculates the percentage of marks in three subjects.

Assuming that the marks in each subject are out of 100, the$totalargument in the function definition is given a default value as 300.

PHP CODE
<?php
   function  percent($p, $c, $m, $ttl=300) {
      $per = ($p+$c+$m)*100/$ttl;
      echo "Marks obtained: \n";
      echo "Physics = $p Chemistry = $c Maths = $m \n";
      echo "Percentage = $per \n";
   }
   percent(50, 60, 70);
?>

Output

It will generate the following result −

PHP CODE
Marks obtained: 
Physics = 50 Chemistry = 60 Maths = 70 
Percentage = 60

However, if the maximum marks in each subject is 50, then you must pass the fourth value to the function, otherwise the percentage will be calculated out of 300 instead of 150.

PHP CODE
<?php
   function  percent($p, $c, $m, $ttl=300) {
      $per = ($p+$c+$m)*100/$ttl;
      echo "Marks obtained: \n";
      echo "Physics = $p Chemistry = $c Maths = $m \n";
      echo "Percentage = $per \n";
   }
   percent(30, 35, 40, 150);
?>

Output

It will produce the following output −

PHP CODE
Marks obtained: 
Physics = 30 Chemistry = 35 Maths = 40 
Percentage = 70