For loop in while loop - php

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

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++
}

for loop inside if condition in 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.

Function calls within a loop when its not necessary PHP

This might be a very dumb question but it still bugs me. I tend to use this:
$variable = $someObject->getSomeValue();
for ($i = 0; $i < $x; $i++) {
performSomeFunction($variable);
}
I have seen people do the following & some of my co-workers are arguing with me that there is no difference in performance if I use function calls within a loop & that it will save 1 line of code.
for ($i = 0; $i < $x; $i++) {
performSomeFunction($someObject->getSomeValue());
}
Now for $x = 10 this might not have impact on performance but what if $x = 10000 ? Which one is the preferred way or best practice in PHP and in Programming general?
Example of function getSomeValue
// just a getter method for the class
function getSomeValue() {
return $this->userFirstName;
}
Highly depends on your function. For example this
function() {
return 2;
}
It won't matter.
For this:
function() {
$pdo->query('SELECT ....');
// more db stuff
return $someDbStuff;
}
It will matter extremely!
Depends on what you do on $someObject->getSomeValue();. If just returns a variable, it doesn't have any impact on performance, if, on the other hand, you are retrieving the data from a database, its very important to avoid it.
However, its always a good policy to avoid unnecessary iterations and do like this
$variable = $someObject->getSomeValue();
for ($i = 0; $i < $x; $i++) {
performSomeFunction($variable);
}
Yeah, sure there's an impact in the performance.
If you are about to use the same value over iterated loops, why would you go get it every time?
It's better getting it before the loop (if there's no chance of changing this value while in the loop) and then reusing it.
Imagine this getSomeValue() needs to access a database or a webservice, would you rather do it 1 time or $x times for the same effect?

How to break out of a foreach once a condition is met?

I have a situation where when dealing with an object I generally use a foreach to loop through it like this:
foreach ($main_object as $key=>$small_object) {
...
}
However, I need to put a conditional in there like this:
foreach ($main_object as $key=>$small_object) {
if ($small_object->NAME == "whatever") {
// We found what we need, now see if he right time.
if ($small_object->TIME == $sought_time) {
// We have what we need, but how can we exit this foreach loop?
}
}
What is the elegant way to do this? It seems wasteful to have it keep looping through if it's found a match. Or is there another approach to do this that is better? Possibly using for instead of foreach?
From PHP documentation:
break ends execution of the current for, foreach, while, do-while or switch structure.
So yes, you can use it to get out of the foreach loop.
Use the break statement inside the if condition:
if ($small_object->TIME == $sought_time) {
break;
}
break statement will break out of the loop.
$count = 0;
......
..
..
..
..
......
if (++$count == 5) break;

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++;
}

Categories