Asked  7 Months ago    Answers:  5   Viewed   37 times

By default a PHP function uses $_GET variables. Sometimes this function should be called in an situation where $_GET is not set. In this case I will define the needed variables as parameter like: actionOne(234)

To get an abstract code I tried something like this:

function actionOne($id=$_GET["ID"])

which results in an error:

Parse error: syntax error, unexpected T_VARIABLE

Is it impossible to define an default parameter by using an variable?


The actionOne is called "directly" from an URL using the framework Yii. By handling the $_GET variables outside this function, I had to do this on an central component (even it is a simple, insignificant function) or I have to change the framework, what I don't like to do.

An other way to do this could be an dummy function (something like an pre-function), which is called by the URL. This "dummy" function handles the variable-issue and calls the actionOne($id).



No, this isn't possible, as stated on the Function arguments manual page:

The default value must be a constant expression, not (for example) a variable, a class member or a function call.

Instead you could either simply pass in null as the default and update this within your function...

function actionOne($id=null) {
    $id = isset($id) ? $id : $_GET['ID'];

...or (better still), simply provide $_GET['ID'] as the argument value when you don't have a specific ID to pass in. (i.e.: Handle this outside the function.)

Wednesday, March 31, 2021
answered 7 Months ago

Yes, use a closure:

functionName($someArgument, function() use(&$variable) {
  $variable = "something";

Note that in order for you to be able to modify $variable and retrieve the modified value outside of the scope of the anonymous function, it must be referenced in the closure using &.

Wednesday, March 31, 2021
answered 7 Months ago

You could let the first function return an array:

function first() {
    $values = array();
    foreach($list as $item ) {
        $values['variable_' . $item->ID] = $item->title;
        // gives $varible_10 = 'some text'; (10 can be replaced with any number)
    $values['ordinary_variable'] = 'something';
    return $values;

and then:

function second() {
    $values = first();
    foreach($list as $item ) {
        $values['variable_' . $item->ID];
        // getting identical value from first() function

or pass it as parameter:


I would advice against global as this introduces side-effects and makes the code harder to maintain/debug.

Wednesday, March 31, 2021
answered 7 Months ago

You can use a heredoc, which supports variable interpolation, making it look fairly neat:

function TestBlockHTML ($replStr) {
return <<<HTML

Pay close attention to the warning in the manual though - the closing line must not contain any whitespace, so can't be indented.

Tuesday, July 27, 2021
answered 3 Months ago

Try setting the parameters' "default value" to use the same query as the "available values". In effect it provides every single "available value" as a "default value" and the "Select All" option is automatically checked.

Sunday, August 1, 2021
The Coding Wombat
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :