This seems a basic question I know but I've not been able to find the answer. Show Let's assume a basic function:
Now let's assume I want to call that function with the following variables:
I can do this:
asked Jun 4, 2012 at 12:51
4 PHP does not support overloading. Therefore, you cannot skip them in any way if you don't move them to the very right of the list of arguments. A common solution is to set a default value of a different type than expected (i.e. NULL). The actual default value is then set within the function. This approach is not really clean and takes some extra lines of code, but if the situation requires it, you can go with this:
answered Jun 4, 2012 at 13:03
NikoNiko 26.2k8 gold badges93 silver badges109 bronze badges It is possible with PHP 8 now, see this answer for details https://stackoverflow.com/a/64997399/519333 Old answer:
And then
Source: http://www.marco.org/2008/11/11/faking-named-parameters-in-php answered Jun 11, 2012 at 16:39
simPodsimPod 9,93816 gold badges82 silver badges126 bronze badges You need to put
Now you can call:
Otherwise you will have to specify all of the parameters. When calling functions, you must specify all of the parameters from left to right. The only time you can omit parameters is:
answered Jun 4, 2012 at 13:01
nickbnickb 58.5k12 gold badges102 silver badges141 bronze badges Have you thought of passing the variables as an array in some way? In that case you only include the variables you need to update
Update: Also you might use the answered Jun 4, 2012 at 13:05
GustavGustav 1,3631 gold badge12 silver badges24 bronze badges I just wrote this function that lets you call a function by an associative array. I've only tested it on scalar types though, but it should work fine for functions which take in
You can use it like this:
Which outputs:
answered Jun 4, 2012 at 13:53
PaulPaul 137k26 gold badges270 silver badges259 bronze badges More pure code 4 you (working 100%, php>5.3), working with classes and functions.
answered Jun 20, 2013 at 0:43
xercoolxercool 3,7896 gold badges26 silver badges32 bronze badges to skip the values try like this
or if u have the values set then
answered Jun 4, 2012 at 12:57
RinzlerRinzler 2,1391 gold badge27 silver badges44 bronze badges 3 As I mentioned by the comment above, you should always keep the optional variables on the right end, because php arguments evaluated from left to right. However, for this case you can do some trick to get rid of duplicate values as follows:
However, you had better solve this situation by relocating you arguments cleverly. answered Jun 4, 2012 at 13:03
mertmert 1,9222 gold badges21 silver badges41 bronze badges How can I pass multiple variables to a function in PHP?If you can get your hands on PHP 5.6+, there's a new syntax for variable arguments: the ellipsis keyword. It simply converts all the arguments to an array.
Can a function return multiple values PHP?A function can not return multiple values, but similar results can be obtained by returning an array.
How can I assign multiple values to a single variable in PHP?When we talk about storing values in PHP, we talk about the word array. To store multiple values, there are two ways of carrying out the task. One way is to assign each value to a single variable, and the other, much more efficient way, is to assign multiple values to a single variable. That is what we call an array.
How pass multiple values in array in PHP?Create an instance for your view file and use predefined method of a controller file to set variables. Like this way: return new ViewModel(array( 'order_by' => $order_by, 'order' => $order, 'page' => $page, 'paginator' => $paginator, ));
|