-2

How can I return a value without breaking a loop, The purpose of this is to return the increment value of a for loop to represent "attempts" that I want to return to view, for example read JSON that takes attempts I want to display it visually in view.

Example of what I've tried

for($i =0; $i< 15; $i++){
    numOfAttempts($i);
     try{
         file_get_contents('url')
     }catch(){}
}

public function numOfAttempts($i){return $i;}   //Return the attempt count to view
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
Eric
  • 23
  • 4
  • 1
    It's not entirely clear to me what you want. The `for` loop will not be broken since the `return` is performed inside another function. – Willem Van Onsem May 01 '16 at 16:29
  • Yes I know it breaks, I want to display the $i variable live in view when its attempting to read file_get_contents. – Eric May 01 '16 at 16:40

1 Answers1

0

you can't do this in raw PHP. It's a server-side process. It starts, does it's work, ends. Results are sent to a browser. look here for work-arounds:

http://www.htmlgoodies.com/beyond/php/show-progress-report-for-long-running-php-scripts.html

and here

Show progress for long running PHP script

also php is a single thread, you can emulate multithreading though. google php multithreading or look at http://robo.li

though, you CAN for, example, read a part of file (or try an url) then report back to user, then go for another run, but it will be not a parallel process, you'll need to place your display inside your loop

Community
  • 1
  • 1
strangeqargo
  • 1,276
  • 1
  • 15
  • 23