Created
March 3, 2024 20:22
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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