Using Optional Argument with User Defined Functions in PHP
Hi everybody. After a long time, I am here again with my new articles. I thought it can be good to start with a little clue for PHP coders.
Usage of PHP functions is so easy and practical. However, usually we need to write our own functions while we are making our applications.
1.1) User Defined Functions in PHP
Structure 1.0:
The function ends when return() statement is executed.
Example 1.1:
Output of Example 1.1:
There is a simple function that takes factorial of a number.
Example 1.2:
Output of Example 1.2:
1.2) Using Optional Arguments with User Defined Functions in PHP
In some cases we don’t need to write all arguments in functions but if arguments of function are more than one, it is not possible to leave arguments empty. So we will define arguments values while we are defining the function. Let’s make it clear with an example.
Example 1.3: This example will not work!
Output of Example 1.3:
As you see this usage is not valid. Now, we are going to define the second argument in the function.
Example 1.3: This example will work!
Output of Example 1.3:
That’s all. Now you don’t have to write the second argument if you don’t need to… You can also equalize the $y argument to a string or to an integer. It’s up to you!

Recently Typed