How can i print even numbers in php?

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:

for($i=0;$i<10 && $i % 2===0;$i++)
echo $i;

Tried with only double == as well.

And this,

    $i=0;
do echo $i; while($i++<10 && $i % 2 ==0);

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

How can i print even numbers in php?

1

Try to use this code

for( $i=0; $i<=10; $i++ )
{ 
    if( $i%2 == 0 ){
        echo $i;
    }
}

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:

for($i=0; $i<10; $i++) {
    if ($i % 2 !==0 ) {
        break;
    }

    echo $i;
}

0

You can eliminate the second condition of your for loop to prevent the breakage and rely exclusive on a third expression to increment $i by two each iteration.

for($i=0; $i<10; $i = $i + 2) {
    echo $i;
}

02468

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:

for ($i = 0; $i < 10; $i++)
    if ($i % 2 == 0)
        echo $i;

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.
Range can create a range with third parameter step 2.

$arr = range(0,20,2);
Echo implode(" ", $arr);

https://3v4l.org/S3JWV

answered Apr 22, 2018 at 16:53

How can i print even numbers in php?

AndreasAndreas

23.4k5 gold badges29 silver badges62 bronze badges

4

you can use also regular loop and get the evens by formula:

for($i=0; $i<10 ;$i++) {
    $j = $i * 2;
    // do somthing with $j witch loop over 10 first evens...
}

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:

<?php
    if (($number % 2) === 1)
      {
          echo "$number is odd.";
      }
      if (($number % 2) === 0)
      { 
          echo "$number is even." ;
      }
?>

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.

   <html>  
    <body>  
    <form method="post"> 
        <input type="number" name="num1">  
        <input type="number" name="num2"> 
        <input type="submit" name="submit" value="Submit">  
    </form>  
    </body>  
    </html> 
    <?php
    if(isset($_POST['submit'])){
        $num1 = $_POST['num1'];
        $num2 = $_POST['num2'];
        //  while loop that will print number
        while($num1 <= $num2){
            //  here is the condition to check the EVEN number
            if($num1%2!==0){
                echo $num1."<br>";
                }
                // increasing the loope counter by 1
            $num1++;
        }
    }
    ?>

i hope now your problem has been solved..

answered Nov 21, 2019 at 9:38

How can i print even numbers in php?

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 .