How do you print even numbers in php while loop?


While loops are simple blocks that execute repeatedly until the while loop condition is not met.

Here is an example of a loop that is executed a total of 10 times:

$counter = 0;

while ($counter < 10) {
    $counter += 1;
    echo "Executing - counter is $counter.\n";
}

The main difference between for loops and while loops is that for loops are used to iterate over an array or an object, and a while loop will execute an unknown amount of times, depending on variable conditions (for example, until a user has entered the correct input).

Flow statements

Loops can be controlled using the break and continue flow statements, which come in handy in while loops very much. The break statement immediately quits the for loop at the middle of the block, while the continue statement returns to the top of the while loop, re-checking if the loop condition is met as well.

The continue statement

Let's use the previous example, but this time let's add a check to see if the number is even. If it is, we will skip it, so that only odd numbers will be printed out.

$counter = 0;

while ($counter < 10) {
    $counter += 1;

    if ($counter % 2 == 0) {
        echo "Skipping number $counter because it is even.\n";
        continue;
    }

    echo "Executing - counter is $counter.\n";
}

The break statement

Let's assume we want to add another test that checks if the counter variable is no larger than 8. If it is, we would like to stop the loop. This will cause the number 9 to be not printed in this example.

$counter = 0;

while ($counter < 10) {
    $counter += 1;

    if ($counter > 8) {
        echo "counter is larger than 8, stopping the loop.\n";
        break;
    }

    if ($counter % 2 == 0) {
        echo "Skipping number $counter because it is even.\n";
        continue;
    }

    echo "Executing - counter is $counter.\n";
}

Exercise

Use a while loop to print all odd numbers in an array. Use the continue statement to skip loops and avoid printing even numbers.

Remember - you will need to use the \n character sequence at the end of the echo statement to continue to the next line.

Tip: to test whether a number is even, check if the number modulus 2 is equal to zero ($number % 2 == 0).



PHP while loop

PHP while loop used to execute same block of code again and again while a condition is true.

while loop syntax

while(condition)
{
// block of code to be executed
}

?>

In while loop first check the condition, if condition is true then execute a block of code.

Example of php while loop

$i=1;
while($i<=5)
{
echo $i;
$i++;
}

?>

The output is:
12345

In above for while example, first set variable $i=1, the condition is $i<=5,
for $i=1 the condition is true then execute code print $i.

Then increment 1 to $i, ,
now $i=2 the condition is true,
$i=3 condition is true,
$i=5 condition is true.
When $i=6 the condition $i<=5 is not true as 6 is not less than or equal to 5.
The while loop true for five times and the result is : 12345

Example

$i=5;

while($i>0)
{
echo $i;
$i – -;
}

?>

The output is: 54321

Here, we set variable $i=5 and check condition $i>0 after each time decrements 1 to variable $i while a condition is true.

Example – PHP while loop

$name=”meera”;
$i=1;
while($i<=5)
{
echo “Hello “. $name . “
”;
$i++;
}

?>

The output is:
Hello meera
Hello meera
Hello meera
Hello meera
Hello meera

while loop example

$i=0;
$sum=0;
while($i<=5)
{
$sum=$sum+$i;
$i++;
}

echo “The sum of 1 to 5 = “.$sum;

?>

The output The sum of 1 to 5 = 15

while loop Example

$i=1;
while($i<=10)
{
echo $i;
$i=$i+2;
}

?>

The output 13579

Example to display odd and even no of given number using while loop.

$even=””;
$odd=””;
$i=1;

while($i<=20)
{
if($i%2==0)
{
$even = $even . ” ” .$i ;
}
else
{
$odd = $odd . ” ” .$i;
}

$i++;
}

echo “The Even no = ” . $even .”
”;
echo “The Odd no = ” . $odd;

?>

The output

The Even no = 2 4 6 8 10 12 14 16 18 20
The Odd no = 1 3 5 7 9 11 13 15 17 19

While loop example

$i=1;

while($i<=5)
{
$j=1;
while($j<=$i)
{
echo $i;
$j++;
}
echo “
”;
$i++;
}

?>

The output is:
1
22
333
4444
55555

Example

$i=1;
while($i<=5)
{
$j=1;
while($j<=$i)
{
echo $j;
$j++;
}
echo “
”;
$i++;
}

?>

The output is:
1
12
123
12345

PHP while loop example

$i=1;
while($i<=5)
{
$j=5;

while($j>=$i)
{
echo $j;
$j=$j-1;
}

echo “
”;
$i++;
}

?>

The output is:
54321
5432
543
54
5

How do you print even numbers in a while loop?

Given a list of numbers, write a Python program to print all even numbers in given list. Using for loop : Iterate each element in the list using for loop and check if num % 2 == 0. If the condition satisfies, then only print the number. # we can also print even no's using lambda exp.

How can I print even numbers in PHP?

Take a number. Divide it by 2. If the remainder is 0, print number is even..
$number=1233456;.
if($number%2==0).
echo "$number is Even Number";.

How can I print 1 to 10 numbers in PHP?

We can print numbers from 1 to 10 by using for loop. You can easily extend this program to print any numbers from starting from any value and ending on any value. The echo command will print the value of $i to the screen. In the next line we have used the same echo command to print one html line break.

How do you make a while loop in PHP?

PHP while loop can be used to traverse set of code like for loop. The while loop executes a block of code repeatedly until the condition is FALSE. Once the condition gets FALSE, it exits from the body of loop..
while($n<=10):.
echo "$n
";.
endwhile;.