Skip to content

Instantly share code, notes, and snippets.

@jserrao
Last active December 17, 2015 11:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jserrao/f892d18872c8f3d013b3 to your computer and use it in GitHub Desktop.
Save jserrao/f892d18872c8f3d013b3 to your computer and use it in GitHub Desktop.
PHP modulus in action
<?php
// % is modulus; it takes $object, divides by 5 and looks at the remainder
// It's a good way to find the nth item in an array for PHP
$object = 20;
// Case 1
// in this case 20/5 = 4.0, making the remainder 0, satisfying the if case
// result: $boo = true
if ($object % 5 == 0) {
$boo = true;
} else {
$boo = false;
}
// Case 2
// in this case 20/8 = 2.5, making the remainder 0 and not satisfying the if case
// result: $boo = false
if($object % 8 == 0) {
$boo = true;
} else {
$boo = false;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment