2016-08-17 42 views
0

我試圖運行一個php腳本,它會根據當天返回特定圖標的名稱。PHP函數中的多個結構

我已經添加了這種計算復活節(星期日)的特殊方法。

現在,我爲今天(17.08)添加了一個特例,只是爲了測試我的算法。但是,我的$ returnValue不會返回我想要的圖標(即資產/圖標/ logo_halloween.png)...我知道這不是萬聖節,但來吧;-)

我可能做錯了什麼這裏有我的if的結構。如果你能幫助我,我會很高興。提前致謝。

function getIconFileName() 
{ 
$iconPath = "assets/icons/"; 
$returnValue = ".";  


// gets current year and stores it in a variable 
$year = date('Y'); 

// Calcul des dates variables (Pâques) 

// gets the Easter Sunday 
$date_Easter_Sunday = easter_date($year); 

if ((date('m') == (date('m', $date_Easter_Sunday))) && (date('d') == (date('d', $date_Easter_Sunday)))) 
{ 
    // Dimanche de Pâques 
    $returnValue = $iconPath . "logo.png"; 
} 

elseif ((date('m') == 08) && (date('d') == 17))     
// ---> It looks like my code never returns this value (logo_halloween.png) <---- 
{ 
    $returnValue = $iconPath . "logo_halloween.png"; 
} 

// Calcul des dates fixes 

elseif ((date('m') == 01) && (date('d') == 01)) 
{ 
    // Premier jour de l'an 
    $returnValue = $iconPath . "logo.png"; 
} 

elseif ((date('m') == 03) && (date('d') == 21)) 
{ 
    // Premier jour du printemps 
    $returnValue = $iconPath . "logo.png"; 
} 

else 
{ 
    // Ceci est un jour normal 
    $returnValue = $iconPath . "logo.png"; 
} 

echo "Path to icon : " . $returnValue . "<br>"; 

return $returnValue; 
} 
+0

問題尋求調試幫助(「爲什麼不是這個代碼工作嗎?」)必須包括所需的行爲,特定的問題或錯誤以及在問題本身中重現它所需的最短代碼。沒有明確問題陳述的問題對其他讀者無益。請參閱:[如何創建最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)。 –

+0

而我的描述在一開始並沒有用處? – Laurent

+0

「(爲什麼不是這個代碼的工作?」)尋求幫助調試問題「必須包括[...] **需要最短的代碼重現它在問題本身。** [...] –

回答

1

你的問題是在這裏

elseif ((date('m') == 08) && (date('d') == 17))     

更改爲

elseif ((date('m') == 8) && (date('d') == 17))     

,它會工作。當比較date('m')時返回字符串'08'整數08 php將字符串'08'轉換爲整數8,並且8 == 08表達式的結果爲false

或者更好:

elseif (date('m/d') == '08/17')     
1

將其更改爲:

if ((date('m') == 8) && (date('d') == 17))     
{ 
    $returnValue = $iconPath . "logo_halloween.png"; 
} 

爲什麼測試:

if (("08" == 08))     
{ 
    echo "OK 08 == 08"; 
} 

if ((08=="08"))     
{ 
    echo "OK 08 == 08"; 
} 

你會看到屏幕上沒有確定。兩種測試均失敗。關於TypeCasting的全部內容!