Default Parameters

5

I'm having a problem with the parameters in a PHP function.

For example:

function exemplo($par1 = 1, $par2 = 2) {
    return $par1 . " - " . $par2;
}

exemplo();        // 1 - 2
exemplo(3);       // 3 - 2
exemplo(3, 4);    // 3 - 4
exemplo(null, 4); //   - 4

In the last call, I'm trying to pass only the second parameter and keep the first as default , but not what happens (variable gets value null ).

Is it possible to achieve the desired behavior in this case?

    
asked by anonymous 24.04.2015 / 22:21

5 answers

5

You can not skip the first parameter, but what you can do is a condition for null to be worked with the parameter.

function exemplo( $par1 = 1 , $par2 = 2 )
{
    return ( ! isset( $par1 ) ? 1 : $par1 ) . " - " . $par2;
}

exemplo( null , 4 );

output : 1 - 4

I have updated the response to use ternary operator on a line.

    
24.04.2015 / 22:30
3

PHP does not yet have named parameters , as there is no Python , so what can be done () mentioned ) is to pass an array as a parameter to the function.

  

But there are certain disadvantages , such as the available parameters   are not documented in the signature , it is necessary to look   in the function code to find out, another drawback is that it requires more code   to implement, because the default values must be merged with   the values indicated in the function call.

     

The initial implementation of the named parameters can be seen here .

Example:

function exemplo(array $args) {
    $pares = array('par1' => 1, 'par2' => 2);
    $args = array_merge($pares, array_intersect_key($args, $pares));
    list($par1, $par2) = array_values($args);

    return $par1 .' - '. $par2;
}

Usage:

echo exemplo([]) . "\n";                         // 1 - 2
echo exemplo(['par1' => 3]) . "\n";              // 3 - 2
echo exemplo(['par1' => 3, 'par2' => 4]) . "\n"; // 3 - 4
echo exemplo(['par2' => 4]) . "\n";              // 1 - 4

DEMO

    
25.04.2015 / 02:53
2
function exemplo($par1 = null, $par2 = null) {
    $_par1 = null === $par1 ? 1 : $par1;
    $_par2 = null === $par2 ? 2 : $par2;
    return $_par1 . ' - ' . $_par2;
}

Consistent behavior if you use exemplo(null, null)

    
24.04.2015 / 22:32
2

The best way to handle this kind of situation is to use the func_get_args() . The use is simple: You declare the function with no parameters and within the function use func_get_args() to collect the passed parameters. An example would be:

function exemplo() {
   $parametros = func_get_args();
   if(!empty($parametros)) {
       $parametro1 = $parametros[0];
       $parametro2 = $parametros[1];
       ...
       $parametroN = $parametros[N];
   }
}

exemplo();
exemplo('arg1', 'arg2');
exemplo('arg1', 'arg2', ..., 'argn');
    
24.04.2015 / 22:32
1

Look, I do not say it's the right one, however, whenever I need to use parameters with default values, I try to leave them last in the function, as follows:

function exemplo ($arg1, $arg2 = null) {
    // Corpo da função
}

In this way it is easy to control, mandatory parameters must be passed first, and those with default values, finally.

Function calls would look like this:

exemplo($arg1);
exemplo($arg1, $arg2);

There is a new way of doing what is proposed, but as a requirement, you should have installed PHP 5.6 or higher, which would be:

function somar (...$numeros) {
    $soma = 0;
    foreach ($numeros as $numero) {
        $soma += $numero;
    }
    return $soma;
}

... $ numbers - Indicates that the function will have a variable number of parameters, and these will be stored in this variable of type Array

Function calls would look like this:

somar(1, 3, 5);
somar(5, 7, 18, 20, 63, 72, 12, 54);
    
16.05.2015 / 21:25