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?

6 Years
Discussion Span
Last Post by miketurpin

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)
$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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.