views:

84

answers:

11

So i want to do a php while loop atleast 10 times to see if the value changes via a curl request but i only want to loop through a max of 10 times

so when true and loop count is less then 10

any ideas

+1  A: 

Use a for loop.

Bill Karwin
+5  A: 

Use a for loop:

for ($i = 1; $i <= 10; $i++) {
    //stuff
}
JoshD
You can also add a test to see if your proc has succeeded. Eg: `for ($i = 1; $i <= 10 $i++)` assuming that you populate $result with something inside of the loop.
sparkey0
Keep in mind that just doing a loop doesn't take very long. If you're trying to wait for some duration, there are much better means (such as a sleep command)
JoshD
+1  A: 
$just = 0;
do {
    //it
} while ($just++ < 10);
Detect
A: 

It looks like you want a for loop with an extra condition... I assume you're using a variable to store whether it has changed (you said 'when true')..

$keepGoing = true;

for ($x = 0; $x < 10 && $keepGoing == true; $x++) {
    //your looping code.. 

}

The loop will run 10 times, or stop when $keepGoing is set to false.

Fosco
A: 
<?php
    $i = 0;
    while ( $i <= 10) {
    echo $i.'<br/>';
    $i++;
    }
?>
gearsdigital
A: 

Basically two possiblities:

for ( $i = 0; i < 10; i++ )
{
    if ( !CONDITION )
         break;

    // do something
}

$i = 10;
while ( CONDITION && $i-- > 0 )
{
    // do something
}
poke
+1  A: 

Use a for loop

for($i = 0; $i < 10; $i++)
{
    if($somethingChange)
        break;
}
Bang Dao
A: 
$value = get_value(); // get initial value
$changed = FALSE;     // we've only checked once so it hasn't yet changed
$num_checks = 1;      // number of checks so far
$max_checks = 10;     // maximum number of checks

while ($num_checks < $max_checks && !$changed) {

    // check again
    $num_checks++;
    $new_value = get_value();

    // has it changed?
    $changed = ($new_value != $value);
}

if ($changed) {
    echo "value changed from $value to $new_value on check $num_checks\n";
} else {
    echo "value stayed as $value; checked $num_checks times\n";
}
Richard Fearn
+1  A: 

simple

$max = 10;
while($max--){
    if(something_is_ok())
        break;
}
dev-null-dweller
A: 

Best option would be to provide a while loop, checking the curl statement has performed correctly.

$i=0; while($curl_result=false && ++$i<10) { /// do curl stuff /// set curl_result to true when it works }

A: 
while(++$i<10){echo $i;}
Devrim