Viewed   124 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.)

Monday, October 10, 2022

PHP 5.3 added a shortform for the ternary operator:

$default_carat_min = $_COOKIE["diamond-search_caratMin"] ?: "0.25";

Which evaluates to the left side if the left side is true, and evaluates to the right side otherwise.

Prior to 5.3, however, you'd have to use the long form.

Saturday, August 20, 2022

Well, why don't you just test ? ;-)

Note: It is not as easy as you'd think -- read the full answer ;-)

Calling the hello_testing(); function, without setting the variable:


I get no output -- which indicates isset returned false.

Calling the function, after setting the variable:

$conditional_random = 'blah';

I get an output:

foo is inside

Which indicates global works as expected, when the variable is set -- well, one should not have any doubt about that ^^

But note that isset will return false if a variable is set, and null!

See the manual page of isset()

Which means that a better test would be:

function hello_testing() {
    global $conditional_random;


And this displays:


No Notice: the variable exists! Even if null.

As I didn't set the variable outside of the function, it shows that global sets the variable -- but it doesn't put a value into it; which means it's null if not already set outside the function.


function hello_testing() {
    //global $conditional_random;



Notice: Undefined variable: conditional_random

It proves that notices are enabled ;-)

And, if global didn't "set" the variable, the previous example would have given the same notice.

And, finally:

function hello_testing() {
    global $conditional_random;

$conditional_random = 'glop';


string 'glop' (length=4)

(This is to purely to demonstrate my example is not tricked ^^)

Saturday, October 8, 2022

Anonymous functions are available since PHP 5.3. You cannot use them in older versions. Take a look at the docs.

Friday, September 23, 2022

Use None as a default value:

def func(items=None):
    if items is None:
        items = []
    print items

The problem with a mutable default argument is that it will be shared between all invocations of the function -- see the "important warning" in the relevant section of the Python tutorial.

Saturday, October 29, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :