Welcome to Questionaries, where you can ask questions and receive answers from other members of the community.

Let us know at info@questionaries.org

tell me something about WHILE Loops?

7 like 0 dislike
tell me something about WHILE Loops?
asked 1 year ago by webmaster (25,380 points)

2 Answers

3 like 0 dislike
 
Best answer
In PHP there are several different types of loops. Basically what a loop does is evaluate a statement as true or false. If it is true it executes some code and then alters the original statement and starts all over again by re-evaluating it. It continues to loop through the code like this until the statement becomes false.

Here is an example in its simplest form:

 <?php
 $num = 1;
 while ( $num <=10 )
     {
     print $num . " ";
     $num++;
     }
 ?>

Basically what this does is: while a number is greater than or equal to 10 it prints the number. The ++ adds one to the number, however this could also be phrased as $num = $num + 1; Once the number becomes greater than 10, in our case it becomes 11, then it stops executing the code within the {brackets}

Below is an example of how you can combine a loop with a conditional

 <?php
 $num = 1;
 while ( $num <=10 )
     {
     if ($num < 5)
         {
         print $num . " is less than 5 <br>";
         }
     else
         {
         print $num . " is not less than 5 <br>";
         }   
 $num++;
     }
 ?>
answered 1 year ago by monirulislam (51,620 points)
1 like 0 dislike
while loops are the simplest type of loop in PHP. They behave just like their C counterparts. The basic form of a while statement is:

while (expr)
    statement

The meaning of a while statement is simple. It tells PHP to execute the nested statement(s) repeatedly, as long as the while expression evaluates to TRUE. The value of the expression is checked each time at the beginning of the loop, so even if this value changes during the execution of the nested statement(s), execution will not stop until the end of the iteration (each time PHP runs the statements in the loop is one iteration). Sometimes, if the while expression evaluates to FALSE from the very beginning, the nested statement(s) won't even be run once.

Like with the if statement, you can group multiple statements within the same while loop by surrounding a group of statements with curly braces, or by using the alternate syntax:

while (expr):
    statement
    ...
endwhile;

The following examples are identical, and both print the numbers 1 through 10:
<?php
/* example 1 */

$i = 1;
while ($i <= 10) {
    echo $i++;  /* the printed value would be
                   $i before the increment
                   (post-increment) */
}

/* example 2 */

$i = 1;
while ($i <= 10):
    echo $i;
    $i++;
endwhile;
?>
answered 1 year ago by marck_don (191,010 points)

Related questions

14 like 0 dislike
1 answer
10 like 0 dislike
2 answers
9 like 0 dislike
1 answer
asked 1 year ago by webmaster (25,380 points)
7 like 0 dislike
2 answers
asked 1 year ago by webmaster (25,380 points)
9 like 0 dislike
1 answer
asked 1 year ago by lily (17,510 points)