Skip to content

Instantly share code, notes, and snippets.

@gazsp
Last active November 27, 2020 01:06
Show Gist options
  • Save gazsp/e3e87abf5eda9e921faa8112d505c10e to your computer and use it in GitHub Desktop.
Save gazsp/e3e87abf5eda9e921faa8112d505c10e to your computer and use it in GitHub Desktop.
<?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";
@ugogineering
Copy link

Thank you gazsp. This is a finer piece of code than the one I wrote.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment