What do you mean by loop in php?

PHP for Loop

The for loop - Loops through a block of code a specified number of times.

The PHP for Loop

The for loop is used when you know in advance how many times the script should run.

Syntax

for [init counter; test counter; increment counter] {
  code to be executed for each iteration;
}

Parameters:

  • init counter: Initialize the loop counter value
  • test counter: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends.
  • increment counter: Increases the loop counter value

Examples

The example below displays the numbers from 0 to 10:

Example

Try it Yourself »

Example Explained

  • $x = 0; - Initialize the loop counter [$x], and set the start value to 0
  • $x

    Try it Yourself »

    Example Explained

    • $x = 0; - Initialize the loop counter [$x], and set the start value to 0
    • $x

      This will produce the following result −

      Loop stopped at i = 10 and num = 40 
      

      The do...while loop statement

      The do...while statement will execute a block of code at least once - it then will repeat the loop as long as a condition is true.

      Syntax

      do {
         code to be executed;
      }
      while [condition];
      

      Example

      The following example will increment the value of i at least once, and it will continue incrementing the variable i as long as it has a value of less than 10 −

         
         
            
            
         
      
      

      This will produce the following result −

      Loop stopped at i = 10
      

      The foreach loop statement

      The foreach statement is used to loop through arrays. For each pass the value of the current array element is assigned to $value and the array pointer is moved by one and in the next pass next element will be processed.

      Syntax

      foreach [array as value] {
         code to be executed;
      }
      

      Example

      Try out following example to list out the values of an array.

         
         
            
            
         
      
      

      This will produce the following result −

      Value is 1
      Value is 2
      Value is 3
      Value is 4
      Value is 5
      

      The break statement

      The PHP break keyword is used to terminate the execution of a loop prematurely.

      The break statement is situated inside the statement block. It gives you full control and whenever you want to exit from the loop you can come out. After coming out of a loop immediate statement to the loop will be executed.

      Example

      In the following example condition test becomes true when the counter value reaches 3 and loop terminates.

         
         
            
         
         
      
      

      This will produce the following result −

      Loop stopped at i = 3
      

      The continue statement

      The PHP continue keyword is used to halt the current iteration of a loop but it does not terminate the loop.

      Just like the break statement the continue statement is situated inside the statement block containing the code that the loop executes, preceded by a conditional test. For the pass encountering continue statement, rest of the loop code is skipped and next pass starts.

      Example

      In the following example loop prints the value of array but for which condition becomes true it just skip the code and next value is printed.

         
         
            
         
         
      
      

      This will produce the following result −

      Value is 1
      Value is 2
      Value is 4
      Value is 5
      

      What do you mean by PHP explain its loops with example?

      Loops in PHP are used to execute the same block of code a specified number of times. PHP supports following four loop types. for − loops through a block of code a specified number of times. while − loops through a block of code if and as long as a specified condition is true.

      What is loop explain with example?

      A "For" Loop is used to repeat a specific block of code a known number of times. For example, if we want to check the grade of every student in the class, we loop from 1 to that number.

      How do you define a loop?

      In computer science, a for-loop [or simply for loop] is a control flow statement for specifying iteration, which allows code to be executed repeatedly. ... .
      A for-loop has two parts: a header specifying the iteration, and a body which is executed once per iteration..

      How many loops are used in PHP?

      There are four different types of loops supported by PHP.

Chủ Đề