0% found this document useful (0 votes)
35 views17 pages

PHP While Statement

This document discusses PHP while loops. It explains that while loops execute a block of code as long as a condition is true. The syntax, usage, and differences between regular while loops and do-while loops are covered. Examples are provided to demonstrate how while loops work and how to use them to iterate through records from a database query.

Uploaded by

Bushra Fatima
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views17 pages

PHP While Statement

This document discusses PHP while loops. It explains that while loops execute a block of code as long as a condition is true. The syntax, usage, and differences between regular while loops and do-while loops are covered. Examples are provided to demonstrate how while loops work and how to use them to iterate through records from a database query.

Uploaded by

Bushra Fatima
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

PHP While Statement

lecture 11
SE-1st Year
Topics Discussed
 PHP Loops
 While loop statements
 When to use while loops
 Types
 How it works
 Syntax
 Practical Examples
 Summary
PHP Loops
 Often when you write code, you want the same
block of code to run over and over again a certain
number of times. So, instead of adding several
almost equal code-lines in a script, we can use
loops.
 Loops are used to execute the same block of code
again and again, as long as a certain condition is
true.
The while loop statement
 The while statement will execute a block of code
if and as long as a test expression is true.
 If the test expression is true then the code block
will be executed. After the code has executed the
test expression will again be evaluated and the
loop will continue until the test expression is
found to be false
When to use while loops
 While loops are used to execute a block of code
until a certain condition becomes true.
 You can use a while loop to read records returned
from a database query.
Types of while loops

 Do… while - executes the block of code at least


once before evaluating the condition
 While… - checks the condition first. If it
evaluates to true, the block of code is executed as
long as the condition is true. If it evaluates to
false, the execution of the while loop is
terminated.
Syntax
 HERE,
 “while(…){…}” is the while loop block code
 “condition” is the condition to be evaluated by
the while loop
 “block of code…” is the code to be executed if
the condition gets satisfied
How It Works
Practical Example
Output
PHP Do While

 The difference between While… loop and Do…


while loop is do… while is executed at-least once
before the condition is evaluated.
Syntax
 HERE,
 “do{…} while(…)” is the do… while loop block
code
 “condition” is the condition to be evaluated by
the while loop
 “block of code…” is the code that is executed at
least once by the do… while loop
How It Works:
Practical Example
Summary
 While… loop is used to execute a block of code
as long as the set condition is made to be false
 The do… while loop is used to execute the block
of code at least once then the rest of the
execution is dependent on the evaluation of the
set condition
  

You might also like