in my web application i want to search in database and when that is null i should store new data, for checking this operation i'm using do while
statement with another option to check and search from database with limit period as retry
, like with this code:
$retry = 0;
do {
$feed = $feeds[array_rand($feeds)];
$history = InstagramActionsHistory::wherePk($feed->pk)->whereActionName('comment')->first();
$retry++;
} while ($history == null || $retry >= 3);
in this code while
when $history
is null or $retry
is bigger than or equals with 3
statement should be break, but it doesn't work correctly
instead of that this code work correctly:
$retry = 0;
do {
$feed = $feeds[array_rand($feeds)];
$history = InstagramActionsHistory::wherePk($feed->pk)->whereActionName('comment')->first();
$retry++;
if ($history == null || $retry >= 3) {
break;
}
} while (true);
it seems multi condition dont work in while statement
You should use logical operator.
http://php.net/manual/en/language.operators.logical.php
Modified code
<?php
$retry = 0;
do {
$feed = $feeds[array_rand($feeds)];
$history = InstagramActionsHistory::wherePk($feed->pk)->whereActionName('comment')->first();
$retry++;
} while ($history && $retry >= 3);
?>
In first case you will get infinity loop, if cycle loop 3 or more times.
So if you want try to fill $history
at no more, that 3 times, you need to specify condition as: while $history
is NULL
and $retry no more, than 3.
$retry = 0;
do {
$feed = $feeds[array_rand($feeds)];
$history = InstagramActionsHistory::wherePk($feed->pk)->whereActionName('comment')->first();
$retry++;
} while ($history == null && $retry < 3);