search

Home  >  Q&A  >  body text

php loop problem

while($a<1000){

$a++;
foo();

}

If foo() consumes a lot of resources, needs to be connected to the Internet, needs to add, delete, modify and check many databases, etc., do we have to wait for foo() to finish executing before starting the next cycle?

巴扎黑巴扎黑2783 days ago394

reply all(3)I'll reply

  • 滿天的星座

    滿天的星座2017-05-16 13:17:15

    Execution rights issue in yes.php..

    Only after the function is executed, the execution right will be returned to the calling place and the following code will continue to be executed.

    reply
    0
  • 怪我咯

    怪我咯2017-05-16 13:17:15

    Yes. That’s why it’s called single thread

    reply
    0
  • 滿天的星座

    滿天的星座2017-05-16 13:17:15

    The next loop will not be executed until the foo function is executed. PHP is a single-process blocking execution. Asynchronous execution requires swoole extension, or use pcntl extension to implement multi-process under Linux, and put time-consuming operations into sub-processes for execution. You can use pthreads extension to implement multi-threading, but it must be executed under the TS version of PHP

    reply
    0
  • Cancelreply