I want to have an if statement in PHP to calculate if an INT equals only certain numbers. The pattern would go like: 3, 5, 8, 10, 13, 15, 18, 20 etc. It adds 3, then 2, repeating. The pattern is consistent with the last digit (3,5,8,0), but I don't want to include the first 0.
I could do it this way, but it could go on forever...
if($int == 3 || $int == 5 || $int == 8 || $int == 10 ....)
{
//do stuff
}
This way also does multiples of 3...
if ($int % 3 == 0)
{
//do stuff
}
But doesn't do the pattern I want. What's the right way of doing this?
You can do it like this.
Get the remainder of the division by 10 and then check for 0, 3, 5 or 8.
if ($int > 0) {
$int = $int % 10;
if ($int == 0 || $int == 3 || $int == 5 || $int == 8)
{
//do stuff
}
}
function check($t) {
$mod = $t % 10;
if ($mod == 0 || $mod == 3 || $mod == 5 || $mod == 8)
echo $t;
}
$arr = array(3, 6, 8, 10, 20, 13, 15, 19);
array_map('check', $arr);