Skip to content

Instantly share code, notes, and snippets.

@damiencarbery
Created November 23, 2022 09:54
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 damiencarbery/2f5d21d29710ee9cc34b3307a8576df5 to your computer and use it in GitHub Desktop.
Save damiencarbery/2f5d21d29710ee9cc34b3307a8576df5 to your computer and use it in GitHub Desktop.
Check whether numbers are divisible by 6 and are at least 6 (for checking the weight of a WooCommerce cart).
<?php
$weights = array( -6, 0, 2, 4, 6, 8, 10, 12, 13, 17, 18, 21, 24, 29, 30, 31 );
$min_weight = 6;
$increments = 6;
foreach ( $weights as $weight ) {
echo "Weight: $weight\n";
// Check if divisible by $increments.
if ( $weight % $increments == 0 ) {
echo "\tMultiple of $increments: Yes\n";
// Verify it is at least $min_weight.
if ( $weight / $min_weight > 0 ) {
echo "\tAbove min weight: Yes\n";
}
}
else {
echo "\tMultiple of $increments: No\n";
}
}
Weight: -6
Multiple of 6: Yes
Weight: 0
Multiple of 6: Yes
Weight: 2
Multiple of 6: No
Weight: 4
Multiple of 6: No
Weight: 6
Multiple of 6: Yes
Above min weight: Yes
Weight: 8
Multiple of 6: No
Weight: 10
Multiple of 6: No
Weight: 12
Multiple of 6: Yes
Above min weight: Yes
Weight: 13
Multiple of 6: No
Weight: 17
Multiple of 6: No
Weight: 18
Multiple of 6: Yes
Above min weight: Yes
Weight: 21
Multiple of 6: No
Weight: 24
Multiple of 6: Yes
Above min weight: Yes
Weight: 29
Multiple of 6: No
Weight: 30
Multiple of 6: Yes
Above min weight: Yes
Weight: 31
Multiple of 6: No
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment