简体   繁体   中英

How can I return a value without breaking a loop

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM