核心代碼:
復制代碼 代碼如下:
<?php
/*
* 計算星座的函數 string get_zodiac_sign(string month, string day)
* 輸入:月份,日期
* 輸出:星座名稱或者錯誤信息
*/
function get_zodiac_sign($month, $day)
{
// 檢查參數有效性
if ($month < 1 || $month > 12 || $day < 1 || $day > 31)
return (false);
// 星座名稱以及開始日期
$signs = array(
array( "20" => "寶瓶座"),
array( "19" => "雙魚座"),
array( "21" => "白羊座"),
array( "20" => "金牛座"),
array( "21" => "雙子座"),
array( "22" => "巨蟹座"),
array( "23" => "獅子座"),
array( "23" => "處女座"),
array( "23" => "天秤座"),
array( "24" => "天蠍座"),
array( "22" => "射手座"),
array( "22" => "摩羯座")
);
list($sign_start, $sign_name) = each($signs[(int)$month-1]);
if ($day < $sign_start)
list($sign_start, $sign_name) = each($signs[($month -2 < 0) ? $month = 11: $month -= 2]);
return $sign_name;
}//函數結束
?>