Hi,

I'd need a function where an optional argument takes on the value of another argument when it's not specified. I'd like to be able to write:

function some_fun ($a, $b, $c, $d=$b) {
...

However, this is not correct PHP syntax. I'd really like it to be, and may suggest it as an enhancement.

Instead, as $d is a string I thought of doing a bit of a workaround like:

function some_fun ($a, $b, $c, $d='COPY $b') {
if ($d = 'COPY $b') {$d = $b;}
...

Not as elegant as having PHP language support, but it'll do for now.

Anyone else needed this?

What is the use case for something like this?
It seems trivial to implement something like this within the function.

function someFucntion($a, $b, $c, $d = null)
{
  //Set $d to $b if $d is not set
  if( is_null($d) === true ){$d = $b;}
  ...
}

and if $d has to always be supplied you could just pass the value of $b to the function a second time.

function someFunction($a, $b, $c, $d)
{
  ...
}
//usage
$a = 2;
$b = 4;
$c = 5;
$value = someFunction($a, $b, $c, $b);

Didn't think of using null and is_null, but that's much better. Thanks very much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.