Last active
November 27, 2020 01:06
-
-
Save gazsp/e3e87abf5eda9e921faa8112d505c10e to your computer and use it in GitHub Desktop.
This file contains 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 | |
// We can use 'int' to declare the type of parameters we require - | |
// no need to use is_int() inside the function | |
function sumMultiplesOf3Or5(int $start, int $end) { | |
$sum = 0; | |
if ($start > $end) { | |
// In a larger program we'd throw an exception here | |
die("$end must be greater than $start"); | |
} | |
for ($i = $start; $i < $end; $i++) { | |
if (($i % 3 == 0) || ($i % 5 == 0)) { | |
// We can sum the values here - no need to create a function for it | |
$sum += $i; | |
} | |
} | |
return $sum; | |
} | |
echo sumMultiplesOf3Or5(0, 10) . "\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you gazsp. This is a finer piece of code than the one I wrote.