2017-07-04 54 views
0

我得到下面的代碼語法錯誤:

function getFridays($year='', $format='', $timezone = 'UTC') { 

$fridays = array(); 
$startDate = new DateTime("{$year}-01-01 Friday", new DateTimezone($timezone)); 

$year++; 
$endDate = new DateTime("{$year}-01-01", new DateTimezone($timezone)); 

$int = new DateInterval('P7D'); 

foreach (new DatePeriod($startDate, $int, $endDate) as $d) { 
    $fridays[] = $d->format($format); 

} 

return $fridays; 
} 

$fridays = getFridays($year, 'Y-m-d', 'America/New_York'); 

Error: unexpected '$fridays' (T_VARIABLE), expecting function (T_FUNCTION)

+0

如果在控制器 – user4419336

+0

上,在codeigniter'$ this-> getFridays($ year,'Ym-d','America/New_York')'上添加您的整個班級,因爲如果我將您的代碼複製到沙箱中,罰款,你可以在這裏看到http://sandbox.onlinephpfunctions.com/code/2d7fc79f19389cc9a5a2379ad546230c56bd8a0d – sintakonte

回答

0

問題是此行

$fridays = getFridays($year, 'Y-m-d', 'America/New_York'); 

你不能把這個功能放在外面並且調用另一個功能。如果你想調用它,你必須把它放在另一個函數中。在for循環之外返回之後,只需嘗試var_dump$friday

var_dump($friday); 
exit; 

你可以看到你的數組。

+0

他的功能是完全沒錯...(http://sandbox.onlinephpfunctions.com/code/2d7fc79f19389cc9a5a2379ad546230c56bd8a0d) – sintakonte

+0

@sintakonte是啊,它完全罰款但我認爲他正在使用codeigniter,因爲codeigniter標籤。在codeigniter中,如果你在控制器中,你不能有這樣的功能。那就是我爲什麼假設。 –

0

提供的代碼工作正常,直接複製到沙箱中,因爲所有類的東西都丟失了。沒有它,它會正常工作。所以,你在方法中聲明變量,但是試圖將它設置在方法之外,這是一個不行的方法。

相關問題