Pause some of the script?

I’m trying to figure out how to pause a function for 2 sec but the rest of the page should run and finish. Is this possible?

I have tried this but with no luck.

echo 'Run this first';

function xml(){
	sleep(10);
	echo 'I am delayed...';
}

xml();

echo 'This should come before the function?!';

Thanks in advance…

PHP is not javascript. The only way, I think, would be to use forking. What’s the purpose behind wanting to do this? There may be a better way.

I’m using a script to store some data in my sql database and then creating xml documents from that… I need the script to tell/pretend another script it is finished when the first part is over and then run the xml thing…

That is not how PHP works.

What should I do then? javascript?, jquery? Any ideas?

What you should do…is not be dependent on such a feature. Why do you need the script to skip that part?

Sounds pretty straight forward to me


doStuff();

storeData();
makeXml();

doMoreStuff();

Maybe you’re looking to have a cron job make any new xml docs later on. That would just be a separate thing. Store the data and be done. Then a second script will run periodically - finding any data it hasn’t processed yet and making the xml.

as QMonkey said.

You’re not wanting a 2 second pause, you’re wanting normal program execution. do A, then B when you’re done with A, then C when you’re done with B.

PHP does not do asynchronous execution unless you’re using fork/exec (IE: Something outside of the script)

Does the xml function need to output anything?

You could send the request to create the XML using a JavaScript <script> element. The request for external javascript still gets called even when javascript is turned off in the browser. (In the browsers I tested it does)

Just calling an external javascript file will execute that file. So you could call a server-side php script from a client-side script element, and as long as it outputs javascript it will execute without creating an error. Even if Javascript is turned off it will still execute the file.

The phrase you are looking for is “asynchronous execution”. Parts of PHP support async operations, most of it doesn’t, and the parts that do won’t function exactly the same on every host.

It is in your user’s best interest to not throw arbitrary delays into code. PHP also has an execution time limit when running on a web host. When PHP runs from the command-line (e.g. via a cron job), there generally is no time limit. This seems like one of those things best left to a long-running cron script to process. Write a temporary file that indicates that the cron script needs to check the database. The cron script checks for that file’s existence every second (PHP has some nuisance issues here - see clearstatcache()) - if so, it runs the SQL query looking for what needs processing, does the processing, and then updates the database to indicate that it is done. Of course, you said there is an XML file that is generated, so you can just check to see if it exists at that point.

The OS is the provider of asynchronous execution in this case instead of trying to do it all from within PHP. Plus, you can simply return to the caller and then use Javascript/jQuery to show a “please wait…” box to the user that pings the server every couple of seconds to see if the process is complete instead of having a web browser hang forever and not fire onready/onload handlers.