Jump to content

Recommended Posts

Posted (edited)

The while loop executes a block of code while a condition is true.

 

$i=0;
while($i < 3) {
echo "Your number is: " .$i. "
";
$i++;
}
?>

 

In this code, while the variable $i is less than 3 the while loop shall run, and the output shall be:

Your number is 0
Your number is 1
Your number is 2
Your number is 3

 

The for loop is used when you know how many times your script should run.

 

for($i=0; $i<=5; $i++) {
echo "Your number is: " .$i. "
";
$i++;
}
?>

 

This example defines a loop with the variable $i=0. The loop shall continue to run as long as $i is less than or equal to 5.

 

Output:

Your number is 0
Your number is 1
Your number is 2
Your number is 3
Your number is 4
Your number is 5

 

Sources: w3schools.com

Edited by TehByte
image.png

"I never see what has been done; I only see what remains to be done.." - Buddha

 

| My Profile | Message Me |

image.pngimage.png

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...