2

So I understand array_merge() combines multiple arrays. I am just really getting confused on the parameters behind the scenes. So how many parameters can it really receive?

I am expecting the answer to be "technically unlimited" (even though I know something would be wrong if you needed to do that).

So following on that idea then, how does the function work correctly? It does not know the parameters ahead of time. Wouldn't the function have to have been designed like so to work:

function array_merge($array, $array1=[], $array2=[], and so on...)

How can we just keep adding parameters?


Note: I have been taking a class that has challenged us to really thing about how functions work, and that is why I am being so specific about how it works.

7
  • maybe it is somehow related to common argument count limit in php? stackoverflow.com/questions/37887906/… Commented Aug 18, 2017 at 21:22
  • Not absolutely sure, but look up variable arguments to a function: stackoverflow.com/questions/1422652/… Commented Aug 18, 2017 at 21:22
  • Basically, default values like that aren't necessary in the function definition, because the function iterates its argument list rather than referring to specific arguments. Commented Aug 18, 2017 at 21:32
  • @FelixGuo After reading your link, I did not realize that was possible. Thank you! Commented Aug 18, 2017 at 21:33
  • 1
    Well, I didn't mean to imply that every PHP function behaves this way. Fixed params certainly are required for some functions. Just not for variadic functions like array_merge. Commented Aug 18, 2017 at 21:39

2 Answers 2

1

Think of it as the same way of sending an array to a function; to size of the array doesn't need to be known in advance, just at the time the function is called.

Then assume that the PHP interpreter does the same trick behind the scenes; a function defined as "I can accept as many parameters as necessary" can be rewritten to just stuff every parameter into an array and give that array to the function (this is a simplification, but conceptually something similar happens), which the function can then read one item at a time.

In fact, you can do this yourself in a PHP function in exactly that way:

<?php
function foo() {
    var_dump(func_get_args());
}

foo(1, 2, 3, 4, 5, 6);

There is nothing magic about this - the interpreter or compiler knows that a function can be called with multiple argument (either through its signature, or as in PHPs case, regardless of definition).

Python would do the same by defining a parameter to "match all indexed arguments" (i.e. not named by a key):

def a(*args):
    print(args)

a(1, 2, 3, 4, 5)
=> (1, 2, 3, 4, 5)

PHP 5.6 introduced the same explicit definition you can use, named variadic functions. These use ... in their signature to tell the interpreter that it should eat all the arguments after the defined ones and put it into an array.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for all of this, it is great. What I was mainly looking for was the variadic functions! I will definitely read into them more!
0

Have you read the documentation of array_merge()?

It accepts any number of arrays as arguments (but at least one):

Description

array array_merge ( array $array1 [, array $... ] )

Merges the elements of one or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array.

It creates a copy of the first array then processes the other arrays one by one, appending their values to the partial result until it processes all input arrays.

User-defined functions can also receive any number of arguments.

3 Comments

I know about the array, I was wondering how it actually can accept the unknown number of parameters.
Many programming languages support the creation of functions with variable number of arguments. It is more difficult (but not impossible) to implement the concept in compiled languages like C and C++ and much easier to do it in interpreted languages.
I guess what confused me is if there is not a set number of parameters then the "suggested" way in PHP I always find is to pass an array. This does open up some functions that I could definitely redo with this concept.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.