Continue skips further processing of current iteration and goes to the next iteration.
is 'continue' what I'm looking for?
-
02-06-2022 - |
Domanda
Given this code:
while($thisrow = mysqli_fetch_array($thisresult)){
if($thisrow['displaythis'] == 'yes'){
echo $thisrow['name'];
} else {
continue;
}
$associatedquery = "select * from database where id = $thisrow['associated']";
$result = mysqli_query($associatedquery);
while($row = mysqli_fetch_array($result)){
echo $row['name'];
}
}
If $thisrow['displaythis'] == no, is it going to 'continue' to process the associatedquery, or is it going to go back to the next value in the first while statement? If not, how can I jump back to the next value in the first while statement and skip the associatedquery? I know I can put the whole 'associatedquery' segment inside the if = 'yes' statement, but I don't want to do that.
Soluzione
Altri suggerimenti
To answer your question, continue will go back to the loop statement and start over with the next value in the loop, it will not execute any of the lower half of your loop after the continue statement. If statements are not loops, but for
, while
, and switch
statements are considered loops in php
So the answer to your question is YES
Here is a more in depth explanation
Q: Is it going to go back to the next value in the first while statement?
A: Yes, because continue
will 'continue' checking for the expected value from the if
condition in the loop and skip the execution of the rest of the code until you break
the loop or it finishes.
Check some examples and usage at: