apotd Posted February 18, 2012 Share Posted February 18, 2012 Hi, I'm totally new to PHP and I'm afraid I don't understand a lot yet. However, I need to make this: when you fill in a number in a form, it should be displayed whether the number is odd or even. I tried the following code, which is obviously not working and I have no idea on how to fix this. <?php $var = $_POST['var']; for ($i = 0; $i < $var; $i++) { if ($i % 2 == 0) { print("even"); } else { print("odd"); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/257275-simple-number-check-problem/ Share on other sites More sharing options...
Pikachu2000 Posted February 18, 2012 Share Posted February 18, 2012 What isn't working about it? It seems fine to me. Quote Link to comment https://forums.phpfreaks.com/topic/257275-simple-number-check-problem/#findComment-1318734 Share on other sites More sharing options...
apotd Posted February 18, 2012 Author Share Posted February 18, 2012 For example, I fill in the number 30. The result I get is: "evenoddevenoddevenoddevenoddevenoddevenoddevenoddevenoddevenoddevenoddevenoddevenoddevenoddevenoddevenodd". Quote Link to comment https://forums.phpfreaks.com/topic/257275-simple-number-check-problem/#findComment-1318736 Share on other sites More sharing options...
Pikachu2000 Posted February 18, 2012 Share Posted February 18, 2012 Well, yeah. That's what it's written to do. What output are you trying to get from it? Quote Link to comment https://forums.phpfreaks.com/topic/257275-simple-number-check-problem/#findComment-1318739 Share on other sites More sharing options...
apotd Posted February 18, 2012 Author Share Posted February 18, 2012 I just want it to say whether the number is odd or even, nothing more. What did I do to make it write all this stuff? Quote Link to comment https://forums.phpfreaks.com/topic/257275-simple-number-check-problem/#findComment-1318742 Share on other sites More sharing options...
litebearer Posted February 18, 2012 Share Posted February 18, 2012 Your script says check each number for zero til it reaches your number and print even/odd for each number. no need for the for loop - just do the number its-self Quote Link to comment https://forums.phpfreaks.com/topic/257275-simple-number-check-problem/#findComment-1318745 Share on other sites More sharing options...
apotd Posted February 19, 2012 Author Share Posted February 19, 2012 My bad, of course. I used the following code instead, which works: <?php $number = $_POST['number']; if($number % 2) { print("odd"); } else { print("even"); } ?> The thing is, I need to perform a for loop on the result (I won't be actually printing it in the end), so I was sort of confused by that. One more thing, is it possible just to put the for loop at the place where the "print" is now situated, or am I thinking totally wrong here? Quote Link to comment https://forums.phpfreaks.com/topic/257275-simple-number-check-problem/#findComment-1318750 Share on other sites More sharing options...
litebearer Posted February 19, 2012 Share Posted February 19, 2012 Not clear as to what you are asking. Can you provide an example Quote Link to comment https://forums.phpfreaks.com/topic/257275-simple-number-check-problem/#findComment-1318754 Share on other sites More sharing options...
apotd Posted February 19, 2012 Author Share Posted February 19, 2012 I'm sorry. I know I sound confusing. Fact is I am confused. But, I found out I had to use a while loop. Tried completing my code but I'm already having problems on the first part. I just want to do a calculation on the number if it is odd, and then make it print the outcome (end1) as long as the number is bigger than one. However, I get an error on the "$number / 2 = $end1;" line. <?php $number = $_POST['number']; if($number % 2) { while ($number > 1){ $number / 2 = $end1; print($end1); } } else { ... } ?> Quote Link to comment https://forums.phpfreaks.com/topic/257275-simple-number-check-problem/#findComment-1318755 Share on other sites More sharing options...
litebearer Posted February 19, 2012 Share Posted February 19, 2012 try changing this... $number / 2 = $end1; to this $end1 = $number / 2; Quote Link to comment https://forums.phpfreaks.com/topic/257275-simple-number-check-problem/#findComment-1318762 Share on other sites More sharing options...
litebearer Posted February 19, 2012 Share Posted February 19, 2012 In fact, get rid of the while if(($number % 2 == 0) && ($number>1)) { $end1 = $number /2; echo $end1; }else{ echo "odd"; } Quote Link to comment https://forums.phpfreaks.com/topic/257275-simple-number-check-problem/#findComment-1318763 Share on other sites More sharing options...
apotd Posted February 19, 2012 Author Share Posted February 19, 2012 When I try that (your second post), it just divides the number in two. I need the script to keep dividing it in two till the number equals one. That's why I thought I should use a while loop to keep it going till that point. I also want it to print every step. But I don't see how I can do this without a loop. Is it wrong to use "$end1 = $number / 2; " as you first said? Because this actually removed the error, but it just prints an awful lot of 0's onto my page. Quote Link to comment https://forums.phpfreaks.com/topic/257275-simple-number-check-problem/#findComment-1318766 Share on other sites More sharing options...
litebearer Posted February 19, 2012 Share Posted February 19, 2012 you while loop is endless. try... while ($number > 1){ echo $number . "<br />"; $number = $number / 2; } Quote Link to comment https://forums.phpfreaks.com/topic/257275-simple-number-check-problem/#findComment-1318767 Share on other sites More sharing options...
apotd Posted February 19, 2012 Author Share Posted February 19, 2012 Works like a charm, thank you! One more thing. I cannot find any serious information on an else statement with a while loop, so I guess this is not possible. However, for an iteration of the code I wanted to create something like this: while ($number % 2){ $number / 2; - i know this is wrong but - } else { $number / 4; } So that, as long as the number is even, it will divide itself by 2, otherwise by 4, and keeps doing this (so I was again thinking of the while loop), but I have no clue on how to do this to be honest. Quote Link to comment https://forums.phpfreaks.com/topic/257275-simple-number-check-problem/#findComment-1318784 Share on other sites More sharing options...
litebearer Posted February 19, 2012 Share Posted February 19, 2012 while loops do not use ELSE in and of themselves - Also you must be careful that you allow some event to occur during a while loop that will eventually make the loop end (or else you will have it looping endlessly). Quote Link to comment https://forums.phpfreaks.com/topic/257275-simple-number-check-problem/#findComment-1318789 Share on other sites More sharing options...
apotd Posted February 19, 2012 Author Share Posted February 19, 2012 Would there be some workaround solution for this? For example another if statement within the while loop? Previously you said I should use this while ($number > 1){ echo $number . "<br />"; $number = $number / 2; } for the particular code part. But I don't really see what made the loop end, since all that was added was the . "<br />, if I am not mistaken. Quote Link to comment https://forums.phpfreaks.com/topic/257275-simple-number-check-problem/#findComment-1318792 Share on other sites More sharing options...
MMDE Posted February 19, 2012 Share Posted February 19, 2012 <?php if(is_numeric($_POST['var'])){ if($_POST['var']%2==0){ echo 'even'; }else{ echo 'odd'; } } ?> There are probably far more effective ways, but I'm not exactly sure how % does it, like looking at the number in binary numbers and see if it ends with 0 or 1, perhaps using one of the bitwise operators. http://php.net/manual/en/language.operators.bitwise.php You could also compare the number divided by 2 and the number divided by 2 and rounded to nearest whole number. Quote Link to comment https://forums.phpfreaks.com/topic/257275-simple-number-check-problem/#findComment-1318794 Share on other sites More sharing options...
litebearer Posted February 19, 2012 Share Posted February 19, 2012 But I don't really see what made the loop end, since all that was added was the . "<br />, if I am not mistaken. while ($number > 1){ echo $number . "<br />"; $number = $number / 2; } the while loop says "Do this as long as $number is greater than 1". inside the while loop, we say "replace the value of $number with whatever the value of $number/2 is". ie. pretend $number has an initial value of 30. first time thru the loop $number is 30 - we echo it - then we replace 30 with 15 second time thru we echo 15 - then replace 15 with 7.5 third time - we echo 7.5 - then replace 7.5 with 3.75 fourth time - echo 3.75 - then replace 3.75 with 1.875 fifth time - echo 1.875 - then replace 1.875 with .9375 Since $number is now .9375, it is less then 1 (the condition we set in our WHILE) so the while loop ends Clear? Quote Link to comment https://forums.phpfreaks.com/topic/257275-simple-number-check-problem/#findComment-1318797 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.