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
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다