If I use a large function that will update data on MySQL and then execute header("Location: somepage"), will PHP wait before all those update queries are done and then redirect or do I have to account for that?
3 Answers
PHP code is executed in a predicable sequential pattern, top down.
If you run some database queries the database server will handle them, PHP's thread doesn't handle the writing itself (just passes the query over).
MyISAM tables are locked on write and and I'm pretty sure the UPDATE queries are atomic (meaning they either all do or don't happen - they don't leave things half finished).
Also, slightly tangential, don't forget to exit after sending the Location header. User agents are free to ignore the Location header.
4 Comments
user1091856
What if it's a move_uploaded_file() before a header()? Will php wait for the upload before redirecting?
alex
@user1091856: That should translate to some system calls which will be called before the header is sent back to the browser (which is irrelevant to moving a file on the server).
Paul Annesley
Yes. More accurately, the upload will have finished before the PHP code even executes.
Frank Farmer
move_uploaded_file() and mysql_query() are both "blocking" methods -- as are most PHP methods. This means the method "blocks" execution until the method is complete, so no other code will run. PHP's documentation will clearly mark "non-blocking" methods which return control before they've completed their work -- these are the exception, not the rule.