Question

I wonder if this is possible somehow …

function get_event_list( $year = date('Y') ) {

    }

So I could call this function like get_event_list(2012), but when not adding a para it always retrieves all events from the current year (2014);

Kind Regards, Matt

Was it helpful?

Solution 3

You could make the parameter nullable like so:

function get_event_list( $year = NULL ){
  $year = is_null( $year) ? date('Y') : $year;
  //Code here
}

A way to call this function would be get_event_list(2012) or get_event_list()

OTHER TIPS

The best way to achieve it is to do:

function get_event_list( $year = null ) {
    if ( is_null($year) ) {
        $year = date('Y');
    }
}

You can't use a built-in function as the default argument.

From the PHP manual:

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

What you need can be achieved as follows:

function get_event_list($year = null) {
    if(!isset($year)) {
        $year = date('Y');
    }
}

You can do it like this

function get_even_list($year = ""){
    if(empty($year)){
        $year = date('Y');
    }

    // Whatever you wann do here
}

Steve

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top