for loop inside if condition in php - php

I need to run set of code inside for-loop only if certain condition is satisfied like:
#for($i = 0; $i <= $date_diffrence; $i++)
#if(condition)
$temp = $i;
#for($multDayCount=0; $multDayCount<count($dayDifferenceArr); $multDayCount++)
$i = $dayDifferenceArr[$multDayCount];
#endif
<code_section>
#if(condition)
#endfor
$i = $temp;
#endif
#endfor
I want the to run regardless of the if- for condition. How can I loop like this? Any help would be greatly appreciated.

Your question as stated is self–answering — if you need to have if inside of a loop you just... can. :)
Looking at your code (which is not quite PHP looking?..) you also seem to want to terminate the loop conditionally?
In PHP loops can be controlled with break and continue. You example looks like it's trying to break the loop conditionally.

Related

I am unable to change for loop into while loop in php?

I am new working on my college project to print some data but I am too confused to changed for loop into while loop.
for($i=0;$i<count($labels);$i++) {
for($j=0;$j<count($dd1);$j++) {
$temp = explode(">",$dd1[$j]);
}
}
I know this question would soon disappear, due to the nature of the question. I understand you are new to programming, so I won't be harsh at you :)
But I would like to help.
To convert any for loop to while in any common programming language (Dont hit me hard for this line)
declare and initiate counter variable outside the loop
while check condition
loop logic
increment counter
Example :
$j = 0;
while($j<count($dd1)) {
// loop logic
$j++
}

adding variable numbers in PHP loop counter

I've got a PHP loop and a counter called $total which is set to 0 before the loop. Then once the loop starts I am adding the value of $weight to the loop counter. I originally did it like this:
$total=0;
foreach ($weights as $weight){
$total = $total+$weight;
}
But realised that it also works like this:
$total=0;
foreach ($weights as $weight){
$total += $weight;
}
Question is which is the correct method or if both are correct which is the better method?
Thanks
Both are correct, and neither is better, strictly speaking. They function identically.
That said, some coding styles prefer += because it's easier to read, while others prefer $x = $x + $y because it requires more deliberate action to write, makes more obvious what is happening, and reduces the likelihood of a single-character typo.

Why This Loop Always Produce N+1 Output?

Let's say I have this inputs :
<input type="hidden" name="block-1" value="001"/>
<input type="hidden" name="block-2" value="012"/>
<input type="hidden" name="block-3" value="002"/>
<input type="hidden" name="block-4" value="005"/>
<input type="hidden" name="block-5" value="008"/>
and I want to process those input using this PHP loop
$i = 1;
do {
$x = 'block-'.$i;
$webBlock = $_POST[$x];
//some codes here
$i++;
}
while (!empty($webBlock));
why I always have 6 outputs? and the last one is blank output. seems that loop always doing n+1. how to make correct loop based on number of inputs given? thanks!
Do you need while?
I'd go with:
$i=0;
foreach($_POST as $name => $value)
{
if( strpos($name , 'block-') !== false ) echo $i . " - " . $name . ": " . $value;
$i++;
}
Believe that should account for items named 'block-n'. The if statement basically says "if block- is anywhere in the name of the field, echo out such and such".
Let me know if you get an error and will amend.
Because you are using a repeat loop, you should use a while loop:
while (!empty($webBlock)){
$x = 'block-'.$i;
$webBlock = $_POST[$x];
//some codes here
$i++;
}
beacause do will get executed at least once what ever may be in while expression .do while is an exit control loop.
Try this:
$i = 0;
do {
$i++;
$x = 'block-'.$i;
$webBlock = $_POST[$x];
//some codes here
}
while (!empty($webBlock));
UPD: The best approach is this:
for ($i = 1; $i <= count($_POST); $i++) {
$webBlock = $_POST['block-'.$i];
//some code here
}
Because You are using Do While loop.I am not sure about your data but i must say that it will execute one more time as you expected.Let us check it by iterating
It will run first time because $webBlock is not empty and same as for 5 times.Now here you want to close the loop but your 5th iteration's condition gets true.Now it will execute once more and show nothing (because there is nothing in $webBlock)and now condition will find that $webBlock is empty.I suggest you to use while loop here.It will solve your issue
I am not a PHP developer so I will just give you general idea about what you are doing.
Actually you are using Do-while loop which is sometimes called Exit Control loop. So, in case of Do-While after executing code your condition will be checked. So, even you didn't get any value in 'webBlock' your code will be executed. So, this is your bug.
Instead of this you can use While loop, Entry control loop. You code is executed if and only if condition is true.
$i = 1;
while (i>0) {
$x = 'block-'.$i;
$webBlock = $_POST[$x];
if(empty($webBlock))
{
break;
}
$i++;
}

Is it bad practice to stuff garbage into $array[-1] to prevent OBOB?

I am trying to write a bunch of nested if statements that are somewhat as follows:
if (strcmp($data['reports'][$i][$j],$data['reports'][$i][$j-1])){
but as you all know, you have to write a separate if statement if it is 0, because 0-1 doesn't exist.
if ($j == 0){
//First report, to prevent OBOB
echo "<br/>".$data['reports'][$i][$j]."";
#-> You would have to write two more statements here to test for province...
}
if ($j >= 1){
//All subsequent reports
if (strcmp($data['reports'][$i][$j],$data['reports'][$i][$j-1])){
echo "<br/>".$data['reports'][$i][$j]."";
#-> You would have to write two more statements here to test for province...
}
Now, imagine if you have to test other dependent values for sequential identicality?
It goes from 4 to 8 to 16 separate statements to just test for 4 of these...
So, I just had the idea to stuff crap into $j = -1.
$data['reports'][$i][-1]='aaaaaaaaaa';
// This would depend on the type of data you are
// comparing I suppose it could be zeros.
$data['provinces'][$i][-1]='aaaaaaaaa';
Is there a better solution?
Why don't you just start your loop with 1 instead of 0? That is, instead of this:
for ($j = 0; $j < count($data['reports'][$i]); $j++ )
do this:
for ($j = 1; $j < count($data['reports'][$i]); $j++ )
It's pointless to compare the first array element to the previous one anyway, so just skip it.
Unless you need to do something with the first element, Vilx's answer is the way to go. By the looks of your code, you always echo the first element, and the others need an if condition. I'm guessing your concerned about duplicate code (good), just move the code that echos a row into a new method and call it conditionally.

For loop in while loop

can I put a for loop in while loop? For example:
while($end = 1)
{
for ($i = 0; $i < count($match); $i++)
{
$mathcas = $match[$i][1];
}
}
Thanks. :)
While it is perfectly possible, I strongly recommend avoiding the particular construct you are trying...
If you find the element you are lloking for, just break the for loop and you will be done!
Edit: and please use == for comparisons!!!
Yes, you can. And now for some padding...
Edit: on second thought, if your internal loop does not change the value of $end, you're looking at an infinite loop.
yes you can.
though in the example you gave the outer while loop will either never end or never run

Categories