thank you for answering the questions inside the comments. So as far as I understand you're running your scirpt behind a webserver.
I would like to point out that it's possible. The magic key here is the HTTP header Transfer-Encoding: chunked
. When Apache does not know the Content-Length
then it will start streaming the response in chunks to the client/browser. With this in mind it should be possible to configure Apache and PHP to support this.
Here is a referencing question which is worth reading to understand the topic: How to make PHP generate Chunked response
Another solution could be to take a look at something like https://reactphp.org. I've used it in the past, contributed to it and know it will solve your task as well but it will lead to infrastructural changes.
Hope this helps you. Please let me know if you have further questions around this topic.