PHP Tutorial on Functions with Undefined Parameters with Stone River eLearning

Beginners in PHP or any other programming language, usually shy away from including functions in their codes as they believe that it makes coding difficult.” If I am using the same operation, why can’t I just copy paste the lines instead of including the operation in a function and then calling the function?” It is quite normal if that question comes to your mind as well, and it seems a better option if you are using a function just twice in a code that runs barely a 100 lines. However; there will be cases, in fact, in the real world applications, you will only be dealing with codes that run into thousands of lines and have the same operation carried out multiple number of times. Will it not be inefficient and quite painful to move back up in the code and then come back to paste the lines of code and then continue with your program. That is not all, every time you copy-paste a certain section of the code, the number of lines in the program go up as well. So not using functions means hard work for you and hard work for the processor as well. Now, even though we have started talking about ‘Why we need to use functions in our codes’, this blog is not about the benefits of function, but about a certain area of functions that might be highly useful in PHP programming.

We have already discussed functions and passing parameters to the functions, however; there is a catch here. When using parameters, a user must mention the parameters as arguments to the function at the time of function call, now, what if we are unsure about the parameters? What if we need undefined parameters in our functions? Confused? Let me explain in a more non-technical manner. Imagine you pass 8 parameters to your functions. Now, you know what you need in the first parameter and the 8th parameter, but at this point in time, you are not sure about the values in the parameters in between. The other parameters will be used some time during the program, but you are just not sure of them right now. What should I do in these cases? This is where the significance of this blog post comes in picture. You might have noticed that we have given a lot of text in this blog without any example, something that we do not usually do. We did it this time because it is quite common to neglect this topic as people think they have already studied functions and this will anyways not be used a lot. However; as they say and we truly believe, you are not and expert if you have neglected things on your way up.

Now, we will directly take you to the implementation of the concept that we discussed in the above paragraph.

Eg:


<?php

function add()

{

$total = 0;

foreach (func_get_args() as $arg)

{

total += (int)args;

}

return $total;

}

echo add (5,10,11,6,8,45,6,9);

?>

Note:

  1. Notice the use of the functions func_get_args() in this example. It ensures that the arguments from the function call are provided to the foreach loop. There is another function called ‘func_num_args’ that can also be used in some cases.
  2. Passing on the parameters in this fashion is called ‘named arguments’ in other programming languages and it is commonly referred as params array in PHP.
  3. There are a few downsides of using the params array when we consider the PHP documentation notion and the IDE, you will realize those as you become more comfortable with PHP scripts and the behind the scenes operations on a PHP script.

 

Online Technology Courses Stone River eLearning
GET YOUR FREE PHP EBOOK!

Comments