Understanding Looping Structures in PHP
Looping structures are essential in programming for writing iteration logics. This article delves into the importance of looping structures, different types in PHP, and creating basic logic using loops like for, while, for each, and do-while. Learn the syntax, parameters, examples, and flow charts to enhance your knowledge on looping structures.
Download Presentation
Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
E N D
Presentation Transcript
Padasalai Chapter - 7 LOOPING STRUCTURE MOHAMED FAKRUDEEN, PGT-COMPUTER SCIENCE SHAMROCK MATRIC. HR. SEC. SCHOOL, MOGAPPAIR, CHENNAI-107
Learning objectives To understand the importance of looping structure. To know different types of looping structure in PHP. To know the basic fundamental logic creation using looping structure.
Looping structure Looping structure are useful for writing iteration logics. It is most feature of many programming languages , including PHP.
Categories of loop They are implemented using the following categories. for loop While loop for each loop Do while loop
For loop: For loop is an important functional looping system which is used for iteration logics when the programmer know in advance how many times the loop should run.
SYNTAX for( init counter; test counter; increment counter) { code to be executed; }
PARAMETERS init counter: Initialize the loop initial value. Test counter: Evaluated for every iteration of the loop. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends. Increment counter: Increases the loop counter value.
EXAMPLE <?php for( $i = 0; $i<= 10; $i++) { echo The numbers is: $i<br> ; } ?>
Foreach loop: Foreach loop is exclusively available in PHP. It works only with arrays. The loop iteration deepens on on each KEY value pair In the array. For each,loop iteration the value of the current array element is assigned to $ value variable and the array pointer is shifted by one, until it reaches the end of the array element.
SYNTAX: foreach ($array as $value) { code to be executed; }
EXAMPLE: <?php $Student_name=array ( Magilan , Iniyan , Nilani , Sibi , Shini ) ; foreach ($Student _name as $value) { echo $value <br> ; } ?>
While loop: While loop is an important feature which is used for simple iteration logics. It is checking the condition whether true or false. It executes the loop if specified condition is true. Refer figure 7.4.
SYNTAX: while ( condition is true) { code to be executed; }
EXAMPLE: <?php $Student_count = 10; $Student_number = 1; while ($Student_number <=$Student _count) { echo The student number is: $Student_number <br> ; $Student_number ++ ; } ?>
Do while loop: Do while loop always run the statement inside of the loop block at the first time execution. Then it is checking the condition whether true or false. It executes the loop if specified condition is true . Refer figure 7.4.
SYNTAX: do { code to be executed; } while (condition
DoWhile loop Structure and Flow Chart
EXAMPLE <?php $Student_count = 10; $Student_number = 1; do { echo The student number is: $Student_number <br> ; $Student_number ++ ; } while ( $Student_number <= $Student_count) ?>
Points to remember PHP while loops execute a block of code while the specified condition is true. The for loop is used when you know in advance how many times the script should run. The foreach loop works only on arrays, and is used to loop through each key/value pair in an array. do...while - loops through a block of code once, and then repeats the loop as long as the specified condition is true
Next topic Forms and Files Forms and Files