[英]PHP: pass internal date() function as function parameter?
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); 因此,我可以像
get_event_list(2012)
这样调用此函数,但是当不添加para时,它总是检索当前年份(2014)中的所有事件;
Kind Regards, Matt 亲切的问候,马特
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 : 从PHP手册 :
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 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()
调用此函数的方法是
get_event_list(2012)
或get_event_list()
You can do it like this 你可以这样
function get_even_list($year = ""){
if(empty($year)){
$year = date('Y');
}
// Whatever you wann do here
}
Steve 史蒂夫
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.