Skip to content

Commit 1522ee9

Browse files
committed
Code optimization
Code optimization
1 parent d5769ed commit 1522ee9

3 files changed

Lines changed: 22 additions & 13 deletions

File tree

demo.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
$thisDay = $today[2];
2727
$date = date_parse($_GET['birthday']);
2828
if($date['error_count'] > 0) {
29-
// Invalid date. Default it to today.
29+
echo "<p style=\"color:red;\"><b><i>Error: Invalid date detected. Defaulting the birthday to today...</i></b></p>";
3030
$year = $thisYear;
3131
$month = $thisMonth;
3232
$day = $thisDay;

demo2.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@
6161
$b_day = 1;
6262
}
6363
$animalIndex = isset($_GET['animal'])?(int)$_GET['animal']:0;
64+
// handling out of range situation
65+
if ($animalIndex < 0) {
66+
$animalIndex += 12;
67+
}
68+
if ($animalIndex > 12) {
69+
$animalIndex %= 12;
70+
}
6471
$today = todayYMD();
6572
$thisYear = $today[0];
6673
$thisMonth = $today[1];

lib_birthday.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@
8383
// Special note: in PHP, avoid declaring function inside function. That will cause "redeclaration error" when the parent function is called more than once.
8484
//
8585

86+
// This function is used by getYearsByAge() and getAnimalYearsByRange()
87+
function swap(&$item1, &$item2) {
88+
$tmp = $item1;
89+
$item1 = $item2;
90+
$item2 = $tmp;
91+
}
92+
8693
// This function is used by getZodiac()
8794
function findIndex($target, $arr, $left=0) {
8895
$max = count($arr)-2;
@@ -156,8 +163,8 @@ function getCNY($year) {
156163
214, 202, 122, 210, 130, 217, 207, 127, 215, 203, 123, 211, 201, 219, 208, 129, 217, 205, 125, 213, 202, 122, 210, 130,
157164
218, 207, 127, 215, 204, 123, 211, 201, 220, 208, 129, 216, 205, 125, 212, 202, 123, 210, 130, 218, 207, 126, 214, 203];
158165

159-
$index = ($year>=1876)?$year-1876:$year;
160-
if(($index < 0) || ($index >= 288)) {
166+
$index = ($year>=1876)?$year-1876:$year; // first year is 1876
167+
if(($index < 0) || ($index >= 288)) { // hardcode 288 for count($arr);
161168
// out of range
162169
return -1;
163170
}
@@ -182,10 +189,8 @@ function passedCNY($index, $month=8, $day=8) {
182189
return TRUE;
183190
}
184191
$cny = getCNY($index);
185-
if ($cny == -1) {
186-
// out of range, default to TRUE
187-
return TRUE;
188-
} else if ($cny <= ($month*100 + $day)) {
192+
if ($cny <= ($month*100 + $day)) {
193+
// this includes condition of $cny == -1 (out of range)
189194
return TRUE;
190195
}
191196
return FALSE;
@@ -223,7 +228,7 @@ function getAnimalIndex($year, $month, $day) {
223228
$index = $year % 12;
224229
} else {
225230
$index = ($year+11) % 12;
226-
}
231+
}
227232
if ($index < 0) {
228233
$index += 12;
229234
}
@@ -276,8 +281,7 @@ function getYearsByAge($age1, $age2, $month=1, $day=1) {
276281
$offset1 = $offset2 = 0;
277282

278283
if ($age1 < $age2) {
279-
// swap them
280-
$age1 ^= $age2 ^= $age1 ^= $age2;
284+
swap($age1,$age2);
281285
}
282286
if (!inthePast($month,$day-1)) {
283287
if ($age1 > 0) {
@@ -296,8 +300,7 @@ function getYearsByAge($age1, $age2, $month=1, $day=1) {
296300
function getAnimalYearsByRange($animalIndex, $year1, $year2, $month=1, $day=1) {
297301
// $animalIndex range from 0 to 11, according to the sequence as listed in getAnimal()
298302
if ($year1 > $year2) {
299-
// swap them
300-
$year1 ^= $year2 ^= $year1 ^= $year2;
303+
swap($year1,$year2);
301304
}
302305
$result = array();
303306
for ($year = $year1; $year <= $year2; $year++) {
@@ -315,7 +318,6 @@ function getAnimalYearsByRange($animalIndex, $year1, $year2, $month=1, $day=1) {
315318
function getAnimalYearsByAge($animalIndex, $age1, $age2, $month=1, $day=1) {
316319
// $animalIndex range from 0 to 11, according to the sequence as listed in getAnimal()
317320
$range = getYearsByAge($age1,$age2,$month,$day);
318-
319321
return getAnimalYearsByRange($animalIndex,$range[0],$range[1],$month,$day);
320322
}
321323

0 commit comments

Comments
 (0)