Continue while loop without including continue statement inside the loop - php

I have a part in my code:
while($a = getrow()){
//code
}
getrow() is a function which keeps on returning array based on some condition.
what getrow() should return so that the while loop doesn't execute the code inside but takes the next value returned by getrow() function.

While loops will run as long as the condition remains true. So as long as you return rows, the code inside will get executed. If you return false, the while loop will terminate. If you want to conditionally avoid running code within the loop, your option is to return something like 'SKIP' and then inside the while loop check if $a == 'SKIP' and then issue a continue.
while($a = getrow()){
if($a == 'SKIP')
continue;
//code
}

You can use continue control structure for skip an iteration. Please read the docs
while($a = getrow()){
if($a == 'something'){
continue; // skip iteration
}
//rest code which you want to run
}

Related

Can i end While loop with function?

I tried to google this and i think it can't be done but i have to ask anyway.
If i have while loop?
while ($smth > 1)
{
func();
}
Is there a way i could exit the while loop immediately with that function inside it?
Maybe that function returns something that stop loop?
I tried
func(){
return break;}
but this doesn't work.
It's not possible the way you've done it -- you can't return a break. But you can return a value that could signal to exit the loop.
function func() {
return true
}
And then
while($smth > 1) {
if(func()) break;
}
This will run the function, but it will also check it's return value to verify it does not need to exit the loop. If the function returned true, the if statement is satisfied and so a break will occur. If the function doesn't return explicitly, the if will not be satisfied.
You sort of can get out of that while loop from inside that function by throwing an exception. Your teammates will not like you anymore, however.

How to continue the outer while loop in a nested if-else condition with PHP?

I have a piece of code as following:
while ( $conver = mysqli_fetch_assoc($converQ)) // for each conversation
{
$group_mark = array();
if ($conver['send_from'] == 1) // send from group
{
if (in_array($conver['sender_id'], $group_mark))
{
continue;
}
else
{
array_push($group_mark, $conver['sender_id']);
}
...
In the beginning of the while-loop, I have to check whether the conversation is from group or individual. If it is from group, I should check whether the id has appeared or not. But now the continue can not stop executing the following code in the while-loop and start the next loop. I think the reason is that its outer function is if-else but not the while-loop directly. Could you please tell me how to edit the code to do what I want? Thank you in advance!
EDIT:
I have tried this: continue 2 but it doesn't work. Why?
Thanks to #ThinkingMedia, I know what's the problem now. I put the $group_mark in the wrong place. Sorry.
In these cases which you want to stop check other rows you can develop a function and return as true in the location which you need:
function check()
{
while ( $conver = mysqli_fetch_assoc($converQ)) // for each conversation
{
$group_mark = array();
if ($conver['send_from'] == 1) // send from group
{
if (in_array($conver['sender_id'], $group_mark))
{
return true;
}
else
{
array_push($group_mark, $conver['sender_id']);
}
}
}
}
Your code is right. But continue does not jump off those if/else statements.
In php docs they say is deprecated: continue {var}
5.4.0 Removed the ability to pass in variables (e.g., $num = 2; continue $num;) as the numerical argument.
You can modify your code as follows:
while ( $conver = mysqli_fetch_assoc($converQ)) // for each conversation
{
$group_mark = array();
if ($conver['send_from'] == 1) // send from group
{
if (!in_array($conver['sender_id'], $group_mark)) {
array_push($group_mark, $conver['sender_id']);
}
}
...
}
Found some interesting, curious about why your $group_mark = array(); is in the while loop. It seem like this will get overwrited and will not able to hit the if condition, because of the group_mark always is empty array.
Try to move this to before of your while loop. This should solve and get what you need.

Break first loop and continue next loop if true

I have a foreach loop inside another foreach loop as this code:
foreach($array1 as $uid => $somevalues) {
$status = true;
foreach($somevalues as $somevalue)
if(!isset($somevalue->$someothervalue))
$status = false;
if($status) {
$content .= "some content added";
$count++;
}
}
How it works
As you see I am looping through all entries in $array1. For each entry I loop through the contained $somevalues array to look for a certain condition (that $someothervalue exists there). Now, if this condition (that something isset) is not true then the $status changes to false.
Only if the $status is still true after all loops of the inner foreach, I wish some action to be made (some content added to a text string $content and a raised counter).
The goal
The arrays I'm working with can be quite large and I have been looking at the break and continue commands to try to skip unnecessary parts to improve the code and have the script running through fewer loops.
As you can see, if a loop from the inner foreach results in $status=false then we can stop it right there (no further loops here are necessary). The break is good for this to skip the rest of the forearch loops.
If this is the case then we can also skip the rest of the current outer loop from the outer foreach, since there will not be run any code now that $status is false. For this purpose the continue command would be useful to let us skip the rest of the current outer loop and go on to the next array row right away.
The question
I have been looking at the command lines break 2 and continue 2 e.g. that let you skip the current AND the outer loop (two steps up) with their respective results (either stopping the loop entirely or skipping the rest of current loop). They work as double break - as a "break break" - and as double continue - as a "continue continue" - respectively.
But here I need something like break continue instead, if you get my point. The first loop should break, the next should continue. I am trying to avoid reaching the if statement in the code if not necessary. The check is already made and it feels like doing an unnecessary double check.
I hope the question is clear. Is there a method for this purpose to double skip from within the inner foreach but with the effect of two different commands break continue?
"Flag" variables are mostly a code smell, and an indication that you actually should be using a function:
function all_valid($somevalues) {
foreach($somevalues as $somevalue)
if(!isset($somevalue->$someothervalue))
return false;
return true;
}
foreach($array1 as $uid => $somevalues) {
if(all_valid($somevalues) {
do stuff
If you're prepared to sacrifice readability for performance, then how about:
foreach($array1 as $uid => $somevalues) {
foreach($somevalues as $somevalue)
if(!isset($somevalue->$someothervalue))
goto next; // I don't believe I wrote this
$content .= "some content added";
$count++;
next:
}
Just use continue 2, it will 'break' out of the first loop and continue the outer loop.
foreach([1, 2, 3] as $i) {
echo $i . PHP_EOL;
foreach([4, 5, 6] as $ii) {
continue 2;
}
echo 'this will never print' . PHP_EOL;
}
Will print:
1
2
3
foreach($array1 as $uid => $somevalues) {
// Task of loop 1
foreach($somevalues as $somevalue){
// Task of loop 2
if(!isset($somevalue->$someothervalue))
{
continue 2;
break;
}
}
}

Why 'break' is not working in array_walk php

Please see my code, i am almost stuck in this, Why the break inside the array_walk not breaking...
$bool=array_walk($_POST, 'check_empty');
function check_empty($item, $key)
{
$bool=(isset($item) && $item != "") ? 1: 0 ;
if(!$bool)
{
//return 0;
break;
}
return $bool;
}
It's not at all clear what you're trying to do, but break is a control structure that exits only true loops (for, foreach, while and do-while) and switch structures.
array_walk is iterative but not a loop in the true sense.
You can't 'break' (to use the terminology) from an array walk callback; it is invoked on each element of the array as a means to update or otherwise modify each element, and I can't imagine a use case where you'd want to terminate this during it.
What are you trying to do? Your break is not working, because you're returning a value right before it - so the line with break is never executed.
If you're trying to remove all empty values from the array, try array_filter($array).

How to stop foreach cycle without stopping the rest of the PHP script?

Having a foreach loop, is it possible to stop it if a certain condition becomes valid?
Example:
<?php
foreach ($foo as $bar) {
if (2+2 === 4) {
// Do something and stop the cycle
}
}
?>
I tried to use return and exit, but it didn't work as expected, because I want to continue executing the remaining of the PHP script.
Use break:
foreach($foo as $bar) {
if(2 + 2 === 4) {
break;
}
}
Break will jump out of the foreach loop and continue execution normally. If you want to skip just one iteration, you can use continue.
http://php.net/manual/en/control-structures.break.php is the answer!
As simple as break;.

Categories