让 PHP 等到函数完成? [英] make PHP wait until a function completes?

查看:42
本文介绍了让 PHP 等到函数完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让 PHP 等到函数返回后再继续?

Is there any way to make PHP wait until a function returns before continuing?

这是我的代码:

<?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';

?>

本来应该等到blahblah.txt改变后,再等五秒,然后打印done",然而,五秒后打印done",不管文件实际上已更改.

It is supposed to wait until blahblah.txt changes, wait another five seconds after that, then print out "done", however, it prints out "done" after five seconds, regardless whether the file actually changed.

推荐答案

PHP 是单线程的,这意味着它在继续之前一次执行一条指令.换句话说,PHP 在继续执行下一条语句之前自然会等待一个函数完成执行.

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.

我尝试在我的机器上执行你的代码,它正确地等待直到我修改文件,然后才完成功能并显示消息.

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.

我什至看不到您的代码中任何可能失败的内容.由于$lfilemod 的创建与while 循环中的检查相同,因此该循环的条件将返回TRUE 和即使文件有问题也执行(filemtime 会在两个错误时返回 FALSE,因此条件将读取 (FALSE == FALSE,这显然是 TRUE).

I can't even see anything in your code that might be failing. Since the creation of $lfilemod is performed in the same way as the check in the while loop, the condition of that loop would return TRUE and execute even if there was a problem with the file (filemtime would return FALSE on both errors, so the condition would read (FALSE == FALSE, which is obviously TRUE).

在运行该循环之前,您的 PHP 脚本是否完全修改了文件?如果是,则 filemtime 返回的初始值可能是脚本最初启动时的修改时间.当您在循环中运行 clearstatcache 时,您将在脚本的前面获取由您的更改引起的新修改时间.

Does your PHP script modify the file at all before running that loop? If it does, then the initial value returned by filemtime might be the modification time from when the script originally started. When you run clearstatcache in the loop, you'll pick up the new modification time caused by your changes earlier in the script.

我的建议:

  • 在设置 $lfilemod 的值之前尝试运行 clearstatcache 以便您知道该值是干净的,并且您正在比较苹果与苹果循环中检查的内容.

  • Try running clearstatcache before setting the value of $lfilemod so that you know the value is clean, and that you're comparing apples-to-apples with what is being checked in the loop.

确保文件确实没有被修改.尝试在代码的开头和结尾放置几行调试行,打印出文件的最后修改时间,然后自己比较这两个行,看看 PHP 是否报告看到修改时间发生了变化.

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.

这不言而喻,但您应该确保 PHP 配置为在开发过程中显示所有错误,以便您可以立即看到出现问题的时间和方式.确保在您的 php.ini 文件中将 display_errors 设置设为 On(或使用 ini_set() 如果您不能修改文件本身),并且您的 error_reporting() 设置为 E_ALL |E_STRICT 用于 PHP <= 5.3,或 E_ALL 用于 PHP 5.4(E_STRICT 是该版本的 E_ALL 的一部分).更好的方法是将错误报告设置为 -1,这样可以有效地打开所有错误报告,而不管 PHP 版本如何.

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_errors setting is turned On in your php.ini file (or use ini_set() if you can't modify the file itself), and that your error_reporting() is set to E_ALL | E_STRICT for PHP <= 5.3, or E_ALL for PHP 5.4 (E_STRICT is part of E_ALL as of that version). A better way is to just set your error reporting to -1 which effectively turns on all error reporting regardless of PHP version.

尝试通过这些修改再次运行您的代码.如果您看到文件确实被修改了,那么您就知道您的代码有效.如果文件没有被修改,你至少应该有一个错误,你可以查一下,或者在这里问我们.

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't being modified, you should at least have an error that you can look up, or ask us here.

这篇关于让 PHP 等到函数完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆