Is there a cleaning function that puts age in a group bucket?

I have this function that classifies the age of a user in a specific age group:

private function calculateAgeGroup($age) { if (!$age) { return null; } if ($age <= 25) { return '0-25'; } if ($age <= 30) { return '26-30'; } if ($age <= 35) { return '31-35'; } if ($age <= 40) { return '36-40'; } if ($age <= 45) { return '41-45'; } if ($age <= 50) { return '46-50'; } if ($age <= 60) { return '51-60'; } return '61-'; } 

Is there a way to simplify (which means: less verbose, less if statements)? My first thought was to use modulo, but I fired it very quickly, as it just doesn't make sense to use modulo here.

The second option will be similar to floor($age/10)*10 . "-" . ceil($age/10)*10 floor($age/10)*10 . "-" . ceil($age/10)*10 floor($age/10)*10 . "-" . ceil($age/10)*10 , but this also does not work in all cases.

The last option that came to my mind would use a series of operators () ? : () ? : , which will make for shorter, but no more readable code. Also not very good.

Does anyone have a good idea how to simplify this? Suggestions appreciated.

+4
source share
4 answers

Try this code:

 function calculateAgeGroup($age) { switch($age) { case $age <= 25: return '0-25'; break; case $age > 50 && $age <= 60: return '51-60'; break; case $age > 60: return '61-'; break; default: return (floor(($age-1)/5)*5+1) . "-" . ceil($age/5)*5; } } 
+5
source

As a base, you can use the following code for your increments:

 private function calculateAgeGroup($age) { if (!$age) return null; for ($i=25; $i<=60; $i+=5) { if ($age <= $i) return ($i-4 > 25 ? $i-4 : 0) . '-' . $i; } return '61-'; } 
+4
source

This should definitely give the desired result. The module was not a good idea:

 function calculateAgeGroup($age) { if (!$age) { return null; } if ($age <= 25) { return '0-25'; } else if ($age > 50 && $age <= 60) { return '51-60'; } else if ($age > 60) { return '61-'; } $age = (($age%5) != 0) ? ($age - ($age%5) + 1) : ($age -= 4); return $age.'-'.($age+4); } 

Another option when the values ​​are pre-computed:

 function calculateAgeGroup($age) { $age = ($age <= 25) ? 0 : ($age-1) - (($age-1) % 5); $age = ($age >= 60) ? 60 : $age; $ages = array ( 0 => '0-25', 25 => '26-30', 30 => '31-35', 35 => '36-40', 40 => '41-45', 45 => '46-50', 50 => '51-60', 55 => '51-60', 60 => '61-', ); return $ages[$age]; } 

I compared the solutions, and the first solution with the module is the fastest of all the answers provided, since there are not many calculations.

This will be even faster if you select an array from a function and make it global or as a (static) class.

 $ages = array ( 0 => '0-25', 25 => '26-30', 30 => '31-35', 35 => '36-40', 40 => '41-45', 45 => '46-50', 50 => '51-60', 55 => '51-60', 60 => '61-', ); function calculateAgeGroup($age) { $age = ($age <= 25) ? 0 : ($age-1) - (($age-1) % 5); $age = ($age >= 60) ? 60 : $age; global $ages; return $ages[$age]; } 
+2
source

You can minimize your code by using a switch case statement or a nested if elseif .

Using switch case :

 function calculateAgeGroup($age) { switch($age){ case $age <= 25: return '0-25'; break; case $age > 60: return '61-'; break; default: if( $age/5 == 0 ) $age = $age - 1; return (floor($age/5)*5).' - '.(ceil($age/5)*5); break; } } 

Using the nested if elseif :

 function calculateAgeGroup($age) { if($age <= 25) return '0-25'; elseif($age > 60) return '61-'; else { if( $age/5 == 0 ) $age = $age - 1; return (floor($age/5)*5).' - '.(ceil($age/5)*5); } } 

OR

 function calculateAgeGroup($age) { if($age <= 25) return '0-25'; elseif($age > 60) return '61-'; else { return ($age%5 == 0)?(floor(($age-1)/5)*5).'-'.(ceil(($age-1)/5)*5) : (floor($age/5)*5).'-'.(ceil($age/5)*5); } } 

I tested the code, and both codes more or less took the same time to execute.

Security Code -

 $time_start = microtime(true); for($i=24;$i<62;$i++) { echo "Age $i :"; echo 'Age Group :'.calculateAgeGroup($i); echo '<br>'; } $time_end = microtime(true); $execution_time = ($time_end - $time_start)/60; //execution time of the script echo '<b>Total Execution Time:</b> '.$execution_time.' Mins'; 
+1
source

Source: https://habr.com/ru/post/1491612/


All Articles