Jump to content


Photo

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


  • Please log in to reply
6 replies to this topic

#1 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 18 March 2006 - 06:48 PM

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


<?
$i=0;
while($i<6){
echo $i;
$i++;
}
?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#2 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 18 March 2006 - 07:27 PM

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.


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


Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#3 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 18 March 2006 - 08:00 PM

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.

<?
$num=10;

while($num <10){

echo "hi";

}else{

if($num < 9){

echo"hello";
}
}
$num++;
?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#4 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 18 March 2006 - 10:04 PM

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.
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#5 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 19 March 2006 - 12:32 AM

[!--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]

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

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

Ken

#6 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 19 March 2006 - 09:13 AM

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

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--]
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#7 Laamgat

Laamgat
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 17 April 2008 - 08:17 PM

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.

<?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;
?>

Can anyone tell me why this is happening?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users