PHP Looping Statements Looping Statements Are Used to Control Repetition in a Program

PHP Looping Statements Looping Statements Are Used to Control Repetition in a Program

PHP Looping Statements Looping statements are used to control repetition in a program. It allows a programmer to execute one or more lines of code repetitively. PHP supports following four loop types: i) while ii) do…while iii) for iv) foreach i) while loop The most basic loop in PHP is the while loop. The purpose of a while loop is to execute a statement or code block repeatedly as long as specified condition is true. Once the specified condition becomes false, the loop will be exited. Syntax of while loop is: while(condition) { code block to be executed } Example: <html> <head> <title>while loop</title> </head> <body> <?php $i = 1; while($i <= 10) { echo "The number is " . $i . "<br />"; $i++; } ?> </body> </html> After the execution of above code, the output would be look like: ii) do while loop The do while loop is a variant of the while loop. This loop will execute the code block once, before checking if the condition is true or false, and then it will repeat the loop as long as the condition is true. Syntax of do while loop is: do { code block to be executed } while (condition); Example: <html> <head> <title>do while loop</title> </head> <body> <?php $i=1; do { echo "The number is " . $i . "<br />"; $i++; } while($i<=5); ?> </body> </html> After the execution of above code, the output would be look like: iii) for loop The for loop is the most compact form of looping and includes the following three important parts: The loop initialization statement where we initialize our counter to a starting value. The initialization statement is executed before the loop begins. The test statement which will test if the given condition is true or not. If condition is true then code given inside the loop will be executed otherwise loop will come out. The iteration statement where you can increase or decrease your counter. You can put all the three parts in a single line separated by a semicolon. Syntax of for loop is: for(initialization statement; test statement; iteration statement) { the code block to be executed } Example: <html> <head> <title>for loop</title> </head> <body> <?php for($i=1; $i<=10; $i++) { echo "The number is " . $i . "<br />"; } ?> </body> </html> After the execution of above code, the output would be look like: iv) foreach loop The foreach loop works only on arrays. 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. Syntax of foreach loop is: foreach ($array as $value) { code to be executed; } Example: <html> <head> <title>foreach loop</title> </head> <body> <?php $colors = array("red","green","blue","yellow","orange"); foreach ($colors as $value) { echo "$value <br>"; } ?> </body> </html> After the execution of above code, the output would be look like: .

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    6 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us