"Porting" my comment to an answer:
This other question is very similar.
PHP, by its default implementation, runs scripts in a single thread so it is impossible to cancel code execution from the script itself.
Only very, very few PHP functions support asynchronous operation, e.g. you can run an asynchronous query with mysqlnd
, however this particular feature is part of the mysql driver, not PHP.
There are extensions which introduce multi-threading to PHP, namely pthread.
To recommend yet another source, read this answer by the author of the extension.
Unfortunately, for a "mainstream" project (which should run as good as possible on as generic setups as possible), using pthreads
is not do-able.
The extension is unlikely to be installed on any shared webspaces.
Also, keep in mind that thread-safe programming (or scripting for those who care) is a serious task and you have to care for it. This means preparing your threaded code to not lock up waiting for each other (deadlock), corrupt each other's data, etc.
A detailed answer on the term of thread-safety can be found here.
Thoroughly think about the work involved, the risks of messing something up, and the gains of using threading.
Make sure your desire is really important enough to introduce complex threading.