Home / Programming / 9. PHP Loop (While, For, Foreach) Definition and Example

9. PHP Loop (While, For, Foreach) Definition and Example

/
/
/
548 Views

This time we will discuss looping in php. Iteration in php has its own characteristics. Regularly when you compose code, you need a similar square of code to run again and again a specific number of times. Thus, rather than including a few practically equivalent code-lines in a content, we can utilize circles.

Circles are utilized to execute a similar square of code over and over, up to a specific condition is valid.

In PHP, we have the accompanying circle types:

  • while – circles through a square of code as long as the predetermined condition is valid
  • do…while – circles through a square of code once, and afterward rehashes the circle as long as the predetermined condition is valid
  • for – circles through a square of code a predetermined number of times
  • foreach – circles through a square of code for every component in an exhibit

The accompanying sections will clarify and give instances of each circle type.

PHP While Loop

The while loop – Loops through a square of code as long as the predetermined condition is valid. See the example below.

The example below displays the numbers from 1 to 5:

Example Explained

  • $x = 1; – Initialize the loop counter ($x), and set the start value to 1
  • $x <= 5 – Continue the loop as long as $x is less than or equal to 5
  • $x++; – Increase the loop counter value by 1 for each iteration

This example counts to 100 by tens:

The PHP Do While Loop

The do…while loop will consistently execute the square of code once, it will at that point check the condition, and rehash the circle while the predefined condition is valid.

Recommended  13. New Performing PHP Form Handling

See the example below. The example below first sets a variable $x to 1 ($x = 1). Then, the do while loop will write some output, and then increment the variable $x with 1. Then the condition is checked (is $x less than, or equal to 5?), and the loop will continue to run as long as $x is less than, or equal to 5:

Note: In a do…while loop the condition is tested AFTER executing the statements within the loop. This means that the do…while loop will execute its statements at least once, even if the condition is false. See example below.

This example sets the $x variable to 6, then it runs the loop, and then the condition is checked:

The PHP For Loop

The PHP for loop is utilized when you know ahead of time how often the content should run.

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

See the example below. The example below displays the numbers from 0 to 10:


Example Explained
  • $x = 0; – Initialize the loop counter ($x), and set the start value to 0
  • $x <= 10; – Continue the loop as long as $x is less than or equal to 10
  • $x++ – Increase the loop counter value by 1 for each iteration

See the example below. This example counts to 100 by tens:

The PHP Foreach Loop

The foreach circle works just on exhibits, and is utilized to circle through each key/esteem pair in a cluster.

For every loop iteration, the value of the current array element is assigned to $value and the array pointer is moved by one, until it reaches the last array element.

See the example below. The following example will output the values of the given array ($colors):


The following example will output both the keys and the values of the given array ($age):

 
  • Facebook
  • Twitter
  • Linkedin
  • Pinterest

Leave a Comment

Your email address will not be published. Required fields are marked *

This div height required for enabling the sticky sidebar
Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views :