Clint C.
Clint C.

Reputation: 688

php function arguments

I don't know how or where I got this idea in my head but for some reason I thought this was possible. Obviously after testing it doesn't work, but is there a way to make it work? I want to set $value2 without having to enter anything at all for $value1.

function test($value1 = 1, $value2 = 2) {

echo 'Value 1: '.$value1.'<br />';
echo 'Value 2: '.$value2.'<br />';

}

test($value2 = 3);

// output
Value 1: 3
Value 2: 2

Upvotes: 4

Views: 2827

Answers (8)

Raphael Michel
Raphael Michel

Reputation: 854

What you're trying to do is called "keyword arguments" or "named arguments" and is not available in PHP in contrast to other scripting languages like Python.

If you have functions with hundreds of parameters and really want to achieve a more flexible solution than what PHP comes with, you could build your own workaround with arrays or objects, maybe in conjunction with func_get_args(). But this obviously isn't as beautiful as real keyword arguments.

Upvotes: 4

pp19dd
pp19dd

Reputation: 3633

For a simple function, it's likely impractical to jump into OOP; however, to throw more fuel to this variation fire, this is a case where you could probably use OOP, for better or worse.

Default function parameters are unwieldy to swap around for your convenience, however, that's not the case with extended classes. Those are meant to be varied and mutated as the need arises.

class test {
    var $value1 = 1;
    var $value2 = 2;

    function __construct() {
        printf( "Value 1 = %s, value 2 = %s\n", $this->value1, $this->value2 );
    }
}

class test2 extends test {
    var $value2 = 42;
}

$me = new test();   // outputs Value 1 = 1, value 2 = 2
$me2 = new test2(); // outputs Value 1 = 1, value 2 = 42

Once again, not the practical solution for simplest of tasks, but, it will allow you to mutate code fragments at will. Rethink whether you can rewrite your code to take advantage of this style.

Upvotes: 1

Starx
Starx

Reputation: 78981

Its not entirely possible the way you want.

Simply,

function test($value1 = null, $value2 = 2) {

echo 'Value 1: '.$value1.'<br />';
echo 'Value 2: '.$value2.'<br />';

}

test(NULL, $value2 = 3);

Or, Use array as parameters

function test($array) {

if(isset($array['value1'])) echo 'Value 1: '.$array['value1'].'<br />';
if(isset($array['value2'])) echo 'Value 2: '.$array['value2'].'<br />';

}

test(array('value2' => 3));

Update:

My another attempt

function test() {
  $args = func_get_args();
  $count = count($args);
  if($count==1) { test1Arg($args[0]); }
  elseif($count == 2) { test2Arg($args[0],$args[1]); }
  else { //void; }
}

function test1Arg($arg1) {
   //Do something for only one argument
}
function test2Arg($arg1,$arg2) {
   //Do something for two args
}

Upvotes: 8

TRiG
TRiG

Reputation: 10643

This is not possible in PHP, as handy as it would be, but there are a couple of ways around it.

function func($val1=null, $val2=null) {
    // Set default values.
    if (!$val1) $val1 = 1;
    if (!$val2) $val2 = 2;
    // Your code here.
}

func(null, 74); // Uses the default value of 1 for $val1.

Or use an array (much handier if you have a very long list of arguments):

function func($arr) {
    // Set default values.
    $val1 = isset($arr[1]) ? $arr[1] : 1;
    $val2 = isset($arr[2]) ? $arr[2] : 2;
    // Your code here.
}

func(array(2 => 74)); // Uses the default value of 1 for $val1.

Actually, I have a function which takes the array, the key, and the default, and returns a value, so I could write those lines as

$val1 = Core::val($arr, 1, 1); // array, key, default
$val2 = Core::val($arr, 2, 2);

Upvotes: 0

Adonais
Adonais

Reputation: 1292

AFAIK, you can't modify the values of the arguments the way you want it. The manual even says this is invalid (http://php.net/manual/en/functions.arguments.php#example-153).

What you're doing withtest($value2 = 3); is first assigning to a new variable $value2 a value of 3 and then sending it as the first parameter of test.

However, you can create a function where when you send a null parameter, it gets a value assigned:

function test($value1 = NULL, $value2 = 2) {

    if ($value1 === NULL)
        $value1 = 1 // $value1 default value

    echo 'Value 1: '.$value1.'<br />';
    echo 'Value 2: '.$value2.'<br />';

}

test(NULL, 3);

// output
Value 1: 1
Value 2: 3

Or well, change the default values of the variables:

function test($value2 = 2, $value1 = 1) {

    echo 'Value 1: '.$value1.'<br />';
    echo 'Value 2: '.$value2.'<br />';

}

test(3);

// output
Value 1: 1
Value 2: 3

Upvotes: 0

KingCrunch
KingCrunch

Reputation: 131881

function test($value1 = null, $value2 = null) {
  $value1 = $value1 ?: 1;
  $value2 = $value2 ?: 2;
  echo 'Value 1: '.$value1.'<br />';
  echo 'Value 2: '.$value2.'<br />';
}

test(null, 3);

Upvotes: 0

dimmat
dimmat

Reputation: 195

Maybe this is what u r looking for:

    function test($value1, $value2) 
    {

      if ($value1!="") 
      {
         echo 'Value 1: '.$value1.'<br />'; 
      }
      else
      {
         echo 'Value 1: 3'<br />'; 
      }
      echo 'Value 2: '.$value2.'<br />';

    }

    test("", $value2 = 3);

    // output
    Value 1: 3
    Value 2: 2

Upvotes: -1

Kristian
Kristian

Reputation: 21820

It is considered bad practice to have arguments with default values come after args that do not. the reason being that its not allowed to simply write a function with a bunch of commas in it.

In your case, you should pass a value to both parameters. Even if the value is null.

Upvotes: -1

Related Questions