Skip to content

Instantly share code, notes, and snippets.

@muathendirangu
Created March 3, 2024 20:22
Show Gist options
  • Save muathendirangu/0096b517a19b54e65d1c585649f0a3dc to your computer and use it in GitHub Desktop.
Save muathendirangu/0096b517a19b54e65d1c585649f0a3dc to your computer and use it in GitHub Desktop.
Here is a PHP function that takes an array of integers and returns the sum of all even numbers in the array
<?php
function sumEvenNumbers(array $numbers): int {
$sum = 0;
foreach ($numbers as $number) {
if ($number % 2 === 0) {
$sum += $number;
}
}
return $sum;
}
// Example usage
$numbers = [1, 2, 3, 4, 5, 6];
$evenSum = sumEvenNumbers($numbers);
echo "The sum of even numbers is: " . $evenSum;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment