1

Is there any nice way to use (potentially) undefined Variables (like from external input) as optional Function parameters?

<?php
$a = 1;

function foo($a, $b=2){
    //do stuff
    echo $a, $b;
}

foo($a, $b); //notice $b is undefined, optional value does not get used.
//output: 1

//this is even worse as other erros are also suppressed
@foo($a, $b); //output: 1

//this also does not work since $b is now explicitly declared as "null" and therefore the default value does not get used
$b ??= null;
foo($a,$b); //output: 1

//very,very ugly hack, but working:
$r = new ReflectionFunction('foo');
$b = $r->getParameters()[1]->getDefaultValue(); //still would have to check if $b is already set
foo($a,$b); //output: 12

the only semi-useful method I can think of so far is to not defining the default value as parameter but inside the actual function and using "null" as intermediary like this:

<?php
function bar ($c, $d=null){
    $d ??= 4;
    echo $c,$d;
}

$c = 3
$d ??= null;
bar($c,$d); //output: 34

But using this I still have to check the parameter twice: Once if it is set before calling the function and once if it is null inside the function.

Is there any other nice solution?

2 Answers 2

2

Ideally you wouldn't pass $b in this scenario. I don't remember ever running into a situation where I didn't know if a variable existed and passed it to a function anyway:

foo($a);

But to do it you would need to determine how to call the function:

isset($b) ? foo($a, $b) : foo($a);

This is kind of hackish, but if you needed a reference anyway it will be created:

function foo($a, &$b){
    $b = $b ?? 4;
    var_dump($b);
}

$a = 1;
foo($a, $b);
Sign up to request clarification or add additional context in comments.

1 Comment

I thought about this option as well. It definitively works but still feels somewhat wrong. I guess there is no better option.
-1

I would do something like this if this was actually a requirement. Just testing with sum of the supplied values just for showing an example.

<?php
$x = 1;

//Would generate notices but no error about $y and t    
//Therefore I'm using @ to suppress these
@$sum = foo($x,$y,4,3,t);  
echo 'Sum = ' . $sum;

function foo(... $arr) {
    return array_sum($arr);
}

Would output...

Sum = 8

...based on the array given (unknown nr of arguments with ... $arr)

array (size=5)
  0 => int 1
  1 => null
  2 => int 4
  3 => int 3
  4 => string 't' (length=1)

array_sum() only sums up 1,4 and 3 here = 8.


Even if above actually works I would not recommend it, because then whatever data can be sent to your function foo() without you having any control over it. When it comes to user input of any kind you should always validate as much as you can in your code before using the actual data from the user.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.