让 PHP 等到函数完成?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14392582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
make PHP wait until a function completes?
提问by markasoftware
Is there any way to make PHP wait until a function returns before continuing?
有没有办法让 PHP 在继续之前等待函数返回?
This is my code:
这是我的代码:
<?php
set_time_limit(0);
function waitforchange($nof) {
$lfilemod=filemtime($nof);
while(filemtime($nof) == $lfilemod) {
clearstatcache();
usleep(10000);
}
}
waitforchange('./blahblah.txt')
sleep(5);
echo 'done';
?>
It is supposed to wait until blahblah.txtchanges, wait another five seconds after that, then print out "done", however, it prints out "done" after five seconds, regardless whether the file actually changed.
它应该等到blahblah.txt更改,然后再等五秒钟,然后打印出“done”,但是,无论文件是否实际更改,它都会在五秒钟后打印出“done”。
回答by AgentConundrum
PHP is single-threaded, which means that it executes one instruction at a time before moving on. In other words, PHP naturally waits for a function to finish executing before it continues with the next statement.
PHP 是单线程的,这意味着它在继续之前一次执行一条指令。换句话说,PHP 在继续执行下一条语句之前自然会等待一个函数完成执行。
I tried executing your code on my machine, and it properly waited and waited until I modified the file before completing the function and displaying the message.
我尝试在我的机器上执行您的代码,它正确地等待并等待我修改文件,然后才能完成功能并显示消息。
I can't even see anything in your code that might be failing. Since the creation of $lfilemodis performed in the same way as the check in the whileloop, the condition of that loop would return TRUEand execute even if there was a problem with the file (filemtimewould return FALSEon both errors, so the condition would read (FALSE == FALSE, which is obviously TRUE).
我什至看不到您的代码中任何可能失败的内容。由于创建 of$lfilemod的方式与while循环中的检查相同,因此TRUE即使文件有问题,该循环的条件也会返回并执行(在两个错误时filemtime都会返回FALSE,因此条件将 read (FALSE == FALSE,即显然TRUE)。
Does your PHP script modify the file at all before running that loop? If it does, then the initial value returned by filemtimemight be the modification time from when the script originally started. When you run clearstatcachein the loop, you'll pick up the new modification time caused by your changes earlier in the script.
在运行该循环之前,您的 PHP 脚本是否完全修改了文件?如果是,则返回的初始值filemtime可能是脚本最初启动时的修改时间。当您clearstatcache在循环中运行时,您将在脚本的早期获取由您的更改引起的新修改时间。
My advice:
我的建议:
Try running
clearstatcachebefore setting the value of$lfilemodso that you know the value is clean, and that you're comparing apples-to-apples with what is being checked in the loop.Make sure the file really isn't being modified. Try placing a couple debugging lines at the start and end of your code which prints out the last modification time for the file, then comparing the two yourself to see if PHP is reporting seeing a change in modification time.
This should go without saying, but you should make sure that PHP is configured to display all errors during development, so you are immediately shown when and how things go wrong. Make sure that the
display_errorssetting is turnedOnin yourphp.inifile (or useini_set()if you can't modify the file itself), and that yourerror_reporting()is set toE_ALL | E_STRICTfor PHP <= 5.3, orE_ALLfor PHP 5.4 (E_STRICTis part ofE_ALLas of that version). A better way is to just set your error reporting to-1which effectively turns on all error reporting regardless of PHP version.
clearstatcache在设置 的值之前尝试运行,$lfilemod以便您知道该值是干净的,并且您正在将苹果对苹果与循环中正在检查的内容进行比较。确保文件确实没有被修改。尝试在代码的开头和结尾放置几行调试行,打印出文件的最后修改时间,然后自己比较这两个行,看看 PHP 是否报告看到修改时间发生了变化。
这不言而喻,但您应该确保 PHP 配置为在开发过程中显示所有错误,以便您立即了解出现问题的时间和方式。确保该
display_errors设置已On在您的php.ini文件中打开(或ini_set()在您无法修改文件本身时使用),并且您error_reporting()的设置E_ALL | E_STRICT为 PHP <= 5.3 或E_ALLPHP 5.4(E_STRICT是E_ALL该版本的一部分)。更好的方法是将错误报告设置为-1有效地打开所有错误报告,而不管 PHP 版本如何。
Try running your code again with these modifications. If you see that the file really is being modified, then you know that your code works. If the file isn'tbeing modified, you should at least have an error that you can look up, or ask us here.
尝试通过这些修改再次运行您的代码。如果您看到文件确实被修改了,那么您就知道您的代码有效。如果文件没有被修改,你至少应该有一个错误,你可以查找,或者在这里问我们。
回答by Emery King
Try assigning the function call to a variable. I think it doesn't wait for the return otherwise.
尝试将函数调用分配给变量。我认为否则它不会等待返回。
回答by Amit
The code looks good; save for a missing semicolon (;) after the waitforchange line. I tested putting the (;) in there and the script behaved as intended. Perhaps that is the culprit? I am at loss though to explain how you got your code to execute at all with that error in there.
代码看起来不错;保存在 waitforchange 行之后缺少的分号 (;)。我测试将 (;) 放在那里,脚本按预期运行。也许这就是罪魁祸首?尽管我无法解释您如何让代码在那里执行该错误。

