function closestToZero(array $numbers) { if (empty($numbers)) { return 0; } $closest = $numbers[0]; foreach ($numbers as $number) { if (abs($number) < abs($closest)) { $closest = $number; } elseif (abs($number) == abs($closest)) { $closest = max($number, $closest); } } return $closest; }
function closestToZero(array $ints) {return 0;}?>- function closestToZero(array $numbers) {
- if (empty($numbers)) {
- return 0;
- }
- $closest = $numbers[0];
- foreach ($numbers as $number) {
- if (abs($number) < abs($closest)) {
- $closest = $number;
- } elseif (abs($number) == abs($closest)) {
- $closest = max($number, $closest);
- }
- }
- return $closest;
- }