Create PHP function with optional parameter or argument

By Faruque Ahamed Mollick

I have already discussed PHP function and told you how to create a function in this article – What is a function in PHP and why we use it? There I have also shown you how to create functions in PHP with the argument which is also known as the parameter.

On that article, you show that after creating a function with an argument, it was necessary to provide the argument whenever you call the PHP function. But what, if you want to make the argument optional? With the process, I discussed before it will return a Fatal error and will tell you that the arguments are expected.

Why we use PHP __autoload() magic function?

How To Redirect URL Using PHP header redirect?

So how to make the arguments in a PHP function optional? How to create your own custom function in PHP with optional arguments or parameters?

Well, there is a way to do it. You can create custom PHP function with optional arguments.

Code snippets to create custom PHP function with optional argument

Below is code snippets to create a custom PHP function where I have declared a default value of argument:

<?php
function FunctionWithOptionalArgs($n = 5)
 {
  echo "The argument value is ".$n;
 }
?>

What happened if I call the function without any argument or parameter inside the function like below:



FunctionWithOptionalArgs();

Will it return a fatal error?

Let’s check it by running the code on your server. You will see the below output:

The argument value is 5

How is it possible? Should it not return a fatal error? But why it runs correctly?

Simple PHP code for file uploading process

Simple PHP Code To Check If URL Parameter Is Exist Or Not

Well, here is the magic. When you declare the value of an argument in the time of creating the function, it will take the value and set the argument in default with that value. Now let’s call the function by putting the argument value:

FunctionWithOptionalArgs(45);

What will happen now?

If you run the code, you will see the below output;

The argument value is 45

So you can see that if you keep the argument empty, it will take the argument with default value you set during creating the function. But when you put argument value, it will replace the default with the provided value in the time of calling the function.

So you can clearly see that the argument now become optional. You can put it empty. When you keep the argument empty, it will take the default value.

Let’s see another example:

<?php
function AddNum($m = 5, $n = 10)
{
  $sum = $m + $n;
  echo $sum;
}
?>

We have created a function with two arguments and also keep it optional by providing the default values. The function will return the sum of these two values. Now, what happens if we call the function without passing any argument –

AddNum();

It will show the result “15“.

In this case, when we call the function without passing any parameter, it takes the argument automatically with those default values we set during creating the function.

Replace Multiple Items In String Using PHP str_replace Function

But if we pass the parameter like below –

AddNum(4, 5);

We will see “9”. Now it takes the parameters as arguments that we set during the function call.

So in this case also we have made the parameters or arguments of the function optional.

I hope from this tutorial you have to understand how to create PHP functions by making the arguments optional.

Leave a Reply

Your email address will not be published. Required fields are marked *