yakoup46 Posted April 12, 2009 Share Posted April 12, 2009 I have been messing around with php and i have a pretty solid, pointless but solid, script but I have a bit of a problem. Here is the script: <html> <title>File Test</title> <body> <form action="<? echo $HTTP_SERVER_VARS['PHP_SELF']; ?>" method="post"> Enter Number <input type="text" name="number"> <input type="submit"> </form> <?php include("values.php"); $i = @$_POST['number']; if(empty($i)) echo "Enter Number Please"; else while($i<50) { if($i%2==0) echo $i . $even_number ."<br />"; else if($i%5==0) echo $i . $divis_5 . "<br />"; else if($i%5==0 && $i%2==0) echo $i . $divis_2_5 . "<br />"; else echo $i . "<br />"; $i++; } ?> </body> </html> The first two if statements work. So like 4 displays $even_number, and 5 displays $divis_5, but I want it to display $divis_5_2 for numbers like 10 and 20. but it instead it displays $divis_5. Any help would be awesome. Thank You Quote Link to comment https://forums.phpfreaks.com/topic/153743-solved-math-problem-and-coding-problem-need-help/ Share on other sites More sharing options...
Daniel0 Posted April 12, 2009 Share Posted April 12, 2009 Then move that conditional up before the other one that checks if $i is divisible by 5. You'll never get to $i%5==0 && $i%2==0 because it'll only evaluate to true if $i%5==0, but if that's true then it'll never be evaluated because it's only evaluated if it's not the case that $i%5==0 Quote Link to comment https://forums.phpfreaks.com/topic/153743-solved-math-problem-and-coding-problem-need-help/#findComment-808014 Share on other sites More sharing options...
yakoup46 Posted April 12, 2009 Author Share Posted April 12, 2009 WOW! That was easy. Thank you very much. However, I thought that PHP was a "loose" language and order didn't matter? I guess it was one continues string of if's. Quote Link to comment https://forums.phpfreaks.com/topic/153743-solved-math-problem-and-coding-problem-need-help/#findComment-808038 Share on other sites More sharing options...
Daniel0 Posted April 12, 2009 Share Posted April 12, 2009 Well, imagine this set of conditionals (pseudo-code): if A: something; else if B: something else; else: something entirely different; This is essentially saying: If it's the case that A then do this: <something> If it's not the case that A, but it's the case that B, then do this: <something else> If it's not the case that B, then do this: <something entirely different> B will only be evaluated if A is false. Quote Link to comment https://forums.phpfreaks.com/topic/153743-solved-math-problem-and-coding-problem-need-help/#findComment-808042 Share on other sites More sharing options...
yakoup46 Posted April 12, 2009 Author Share Posted April 12, 2009 DUH. LOL . Thanx for the help. Quote Link to comment https://forums.phpfreaks.com/topic/153743-solved-math-problem-and-coding-problem-need-help/#findComment-808047 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.