I'm somewhat new to PHP, been reading a few books and I've never seen a loop where it gets you all the even numbers(for example from 1 to 10), so I decided to try it myself: Show
Tried with only double == as well. And this,
Can't seem to figure out how to use 2 conditions in the same statement. Would appreciate the help! Thanks. asked Apr 22, 2018 at 16:48
1 Try to use this code
answered Apr 22, 2018 at 16:53
The loop is breaking entirely when the second condition fails the first time. On the first iteration: 0 is less than 10, and it is even, so the loop iterates. On the second iteration: 1 is less than 10, but is odd, so the loop breaks. Your code is the equivalent of this:
You can eliminate the second condition of your
answered Apr 22, 2018 at 16:56
HPierceHPierce 6,9617 gold badges36 silver badges47 bronze badges The second statement in a for-loop is/are the condition(s) which gets checked every loop. so if it fails your loop stops. what you need will look somewhat like this:
So the loop will run over every number but only print out the even ones. answered Apr 22, 2018 at 16:52
wayneOSwayneOS 1,4391 gold badge11 silver badges19 bronze badges You don't need to loop.
https://3v4l.org/S3JWV answered Apr 22, 2018 at 16:53
AndreasAndreas 23.4k5 gold badges29 silver badges62 bronze badges 4 you can use also regular loop and get the evens by formula:
answered Apr 22, 2018 at 17:10 How to print all even numbers between two input values typed by user. please write code hint only in php. asked Nov 21, 2019 at 9:07
8 I can give you a hint. Lookup the modulo operator. With it you can do something like this:
Also. Have a look at how you ask questions on stack overflow. Or they will prevent you from asking questions in the future if you don't adhere to those rules. answered Nov 21, 2019 at 9:18
JulesJules 671 silver badge8 bronze badges use this code, for user input i have created a form.
i hope now your problem has been solved.. answered Nov 21, 2019 at 9:38
mufazmimufazmi 9624 gold badges17 silver badges34 bronze badges 17 How do you print even numbers in php while loop?php $end=50; $even= "Even Numbers Are : "; $odd="<br /> Odd Numbers Are : "; for($i=1;$i<=$end;$i++) { if($i%2==0) { $even. =$i.","; }else $odd. =$i.","; } echo $even.
How do you check a number is even or odd in php?php // Recursive function to check whether // the number is Even or Odd function check($number){ if($number == 0) return 1; else if($number == 1) return 0; else if($number<0) return check(-$number); else return check($number-2); } // Check the number odd or even $number = 35; if(check($number)) echo "Even"; else echo " ...
How can I print only odd numbers in php?Then we have to Perform the for loop for that first assign the value 1 into the variable i and perform the loop until the condition 'i <=n' becomes false and in the block of the loop we have to check the condition 'i % 2 != 0' if true print the value of the variable i which will be a odd number.
How do you code an even number?If a number is evenly divisible by 2 with no remainder, then it is even. You can calculate the remainder with the modulo operator % like this num % 2 == 0 . If a number divided by 2 leaves a remainder of 1, then the number is odd. You can check for this using num % 2 == 1 .
|