Jump to content

*SOLVED* while loop exsplained and examples please thank you.


redarrow

Recommended Posts

Can someone give more examples please fully exsplained as i am trying to learn the while loop.

As meny examples as possable while loop only thank you

1.set conditon i=0
2.while() {}
3.i<6
4.echo statement
5.i++ meaning 1 more


[code]
<?
$i=0;
while($i<6){
echo $i;
$i++;
}
?>
[/code]
Link to comment
Share on other sites

I found this on the net to learn all the loops properly i gooing to use it as a refrence and learn it all till i goto the next problam what is arrays dam good luck all
I need it lol

If a user could brake each line down and exsplain all then grate thank you so much.

[code]

<html>
  <head>
    <title>Loops</title>
  </head>
  <body>
    <h2>FOR loop</h2>
    <p>We will print "Hello, World!!!" 10 times</p>
    <?php
      for ($i = 0; $i < 10; $i = $i + 1)
      {
        echo "Hello, World!!!<br>";
      }
    ?>
    <h2>WHILE loop</h2>
    <p>We will print "Hello, World!!!" for 0.01 seconds</p>
    <?php
      $counter = 0;
      $start_time = microtime();
      while ($start_time > (microtime() - 0.01))
      {
        echo "Hello, World!!! ";
        $counter = $counter + 1;
      }
      echo "<br>Loop executed " . $counter . " times.";
    ?>
    <h2>DO-WHILE loop</h2>
    <p>We will print "Hello, World!!!" for 0.01 seconds</p>
    <?php
      $counter = 0;
      $start_time = microtime();
      do
      {
        echo "Hello, World!!! ";
        $counter = $counter + 1;
      } while ($start_time > (microtime() - 0.01));
      echo "<br>Loop executed " . $counter . " times.";
    ?>
    <h2>FOREACH loop</h2>
    <p>We will print values from array</p>
    <?php
      $address_book = array (
        "Jessica" => "(415) 555-2946",
        "Michelle" => "(925) 555-1274",
        "Linda" => "(707) 555-3349" );
      foreach($address_book as $name=>$phone)
        echo $name . " - " . $phone . "<br>";
    ?>
    <h2>BREAK and CONTINUE</h2>
    <p>We will print as many even number, which can be divided by 4, in 0.01 second</p>
    <?php
      $start_time = microtime();
      for ($i = 1; $i <= 1000000; $i = $i + 1)
      {
        if ($i % 2 == 1)
          continue;
          // Number is odd, there is no point to continue
          // loop to check if it can be divided by 4
          // so we are going to next loop iteration

        if ($i % 4 == 0)
          echo "Number " . $i . " is dividable by 4<br>";
        
        if ($start_time < (microtime() - 0.01))
          break;
          // If we are out of time we will stop the loop
          // Notice: at this point condition is still true
          //         but the loop quits
      }
    ?>
  </body>
</html>

[/code]
Link to comment
Share on other sites

please help dont get it at all num=10 while num less then ten echo hi else if num less then 9 echo hello.


I am trying to use the while loop and if in the same code how cheers.
[code]

<?
$num=10;

while($num <10){

echo "hi";

}else{

if($num < 9){

echo"hello";
}
}
$num++;
?>
[/code]
Link to comment
Share on other sites

how can u count from 1 to 10 with the while loop and for loop and while do loop.

example using all the statments in one.

for echo 1

while echo 2

while do echo 3

for echo 4

ect ect ect

i tried and faild 3 hours please help thank you.

the idear is to echo the 1 to 10 out useing one varable $num = 10;
and use all the loops and display 1-10

THANK YOU.
Link to comment
Share on other sites

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]how can u count from 1 to 10 with the while loop and for loop and while do loop.[/quote]

[code]<?php
//
// for loop
//
for($i=1;$i<11;$i++) echo $i.'<br />';
//
// while loop
//
$i = 1;
while ($i<11) {
    echo $i.'<br />';
    $i++; }
//
//
?>[/code]

I've never used the "while do" loop structure, since it is just slightly different from a while loop.

Ken
Link to comment
Share on other sites

[!--quoteo(post=356295:date=Mar 19 2006, 12:32 AM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Mar 19 2006, 12:32 AM) [snapback]356295[/snapback][/div][div class=\'quotemain\'][!--quotec--]
[code]<?php
//
// for loop
//
for($i=1;$i<11;$i++) echo $i.'<br />';
//
// while loop
//
$i = 1;
while ($i<11) {
    echo $i.'<br />';
    $i++; }
//
//
?>[/code]

I've never used the "while do" loop structure, since it is just slightly different from a while loop.

Ken
[/quote]

thanks ken getting there cheers.

[!--sizeo:5--][span style=\"font-size:18pt;line-height:100%\"][!--/sizeo--]solved[!--sizec--][/span][!--/sizec--]
Link to comment
Share on other sites

  • 2 years later...
I'm working on a script that will display a random image each week.  The script calls for a random number, but I don't ever want the number to be the same after being executed twice.  So, I've tried while loops and do-while loops and every time, the script just freezes and continues to loop continuously.

[code]<?php

// Get current week number.  Assign to a variable.
$current_week = date("W");

// Open up and read current_week.txt.  Assign to a variable.
$previous_week_array = file("current_week.txt");
$previous_week = $previous_week_array[0.1];

// Open up and read current_shirt.txt.  Assign to a variable.
$current_shirt_array = file("current_shirt.txt");
$current_shirt = $current_shirt_array[0];

if ($current_week > $previous_week) {
$random_number = mt_rand(1,7);
while ($randon_number = $current_shirt)
$random_number = mt_rand(1,7);
}

echo $random_number;
?>[/code]

Can anyone tell me why this is happening?
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.