phpQuestioner Posted November 26, 2007 Share Posted November 26, 2007 How can I compare all prime numbers (number divisible by 3) in an array against against a variable in a if condition? example <?php $checker = array("3","6","9","12","15"); if ($skip == in_array($checker)) { // do something } ?> Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted November 26, 2007 Share Posted November 26, 2007 use the "%" operator consider the following code --Modified-- for ($i=1;$i<=15;$i++) { if (($i%3) == 0) { echo $i; } } Quote Link to comment Share on other sites More sharing options...
BenInBlack Posted November 26, 2007 Share Posted November 26, 2007 ??? In mathematics, a prime number (or a prime) is a natural number which has exactly two distinct natural number divisors: 1 and itself. Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted November 26, 2007 Share Posted November 26, 2007 hmm.. come to think about it, it is did not think about that when I provided the a solution , just provided the requested code Quote Link to comment Share on other sites More sharing options...
BenInBlack Posted November 26, 2007 Share Posted November 26, 2007 your code was fine , I just wanted to point that out in case he had a misconception of what prime numbers are Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted November 26, 2007 Author Share Posted November 26, 2007 well the thing of it is; I am trying to check the to see if my "skip" variable is equal to 30, 60, or 90 with an array and an if condition. what is the best way for me to do this? i guess prime numbers wasn't what I meant. Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted November 26, 2007 Share Posted November 26, 2007 your if statement should be if (($variable%30) != 0) { do something } Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted November 26, 2007 Author Share Posted November 26, 2007 that doesn't quit do what I am trying to accomplish here is what I have: for ($i = 0; $i < $tp; $i++) { $display="15"; $pganteqty = $display * $i; $ci = $i + 1; if($ci<=3) { $checker1 = $display * 3; $checker2 = $display * 6; if (($skip >= $checker1) && ($skip <= $checker2)) { echo " <a href=\"$thispage?skip=". ($pganteqty + ($display * 3)) ."\">". ($ci + 3) ."</a> "; } else { echo " <a href=\"$thispage?skip=$pganteqty\">$ci</a> "; } } } now instead of using a lot of if else conditions to accomplish this; I want to figure out how to do this every time my "skip" variable is increases by 30 (30,60,90,120, and so on.........). Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.