Viewed   84 times

Most of us know the following syntax:

function funcName($param='value'){
    echo $param;

Result: "value"

We were wondering how to pass default values for the 'not last' paramater? I know this terminology is way off, but a simple example would be:

function funcName($param1='value1',$param2='value2'){
    echo $param1."n";
    echo $param2."n";

How do we accomplsh the following:

funcName(---default value of param1---,'non default');


not default

Hope this makes sense, we want to basically assume default values for the paramaters which are not last.




PHP doesn't support what you're trying to do. The usual solution to this problem is to pass an array of arguments:

function funcName($params = array())
    $defaults = array( // the defaults will be overidden if set in $params
        'value1' => '1',
        'value2' => '2',

    $params = array_merge($defaults, $params);

    echo $params['value1'] . ', ' . $params['value2'];

Example Usage:

funcName(array('value1' => 'one'));                    // outputs: one, 2
funcName(array('value2' => 'two'));                    // outputs: 1, two
funcName(array('value1' => '1st', 'value2' => '2nd')); // outputs: 1st, 2nd
funcName();                                            // outputs: 1, 2

Using this, all arguments are optional. By passing an array of arguments, anything that is in the array will override the defaults. This is possible through the use of array_merge() which merges two arrays, overriding the first array with any duplicate elements in the second array.

Sunday, August 7, 2022

My suggestion is use array instead of using number of argument, For example your function call should be like this.

$params[6] = 'value';

For identify that sixth parameter has set

function MyFunction($params){
 If ( isset($params[6]) ) // parameter six has value


I hope that it will be a alternate way

Friday, October 7, 2022

You need to do that yourself. You can use null to indicate that a default value should be used:

public function getSomething($orderBy = null, $direction = null, $limit = null) {
    // fallbacks
    if ($orderBy === null) $orderBy = 'x';
    if ($direction === null) $direction = 'DESC';

    // do something random

Then pass null when calling it to indicate that you want to use the defaults:

$random = $this->my_model->getSomething(null, null, 10);

Another possible solution that I use sometimes is an additional parameter at the very end of the parameter list, containing all optional parameters:

public function foo($options = array()) {
    // merge with defaults
    $options = array_merge(array(
        'orderBy'   => 'x',
        'direction' => 'DESC',
        'limit'     => null
    ), $options);

    // do stuff

That way you do not need to specify all optional arguments. array_merge() ensures that you are always dealing with a complete set of options. You would use it like this:

$random = $this->my_model->foo(array('limit' => 10));

It seems like there is no required parameter this particular case, but if you need one, simply add it in front of the optional ones:

public function foo($someRequiredParameter, $someOtherRequiredParameter, $options = array()) {
    // ...
Tuesday, October 4, 2022

A function can have multiple signatures if the signatures differ in arity. You can use that to supply default values.

(defn string->integer 
  ([s] (string->integer s 10))
  ([s base] (Integer/parseInt s base)))

Note that assuming false and nil are both considered non-values, (if (nil? base) 10 base) could be shortened to (if base base 10), or further to (or base 10).

Monday, September 26, 2022

You can initialize the property in the default constructor:

public MyClass()
   IsPopup = true;

With C# 6.0 it is possible to initialize the property at the declaration like normal member fields:

public bool IsPopup { get; set; } = true;  // property initializer

It is now even possible to create a real read-only automatic property which you can either initialize directly or in the constructor, but not set in other methods of the class.

public bool IsPopup { get; } = true;  // read-only property with initializer
Tuesday, December 13, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :