Skip to content

Instantly share code, notes, and snippets.

@jserrao
Last active Dec 17, 2015
Embed
What would you like to do?
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