php 脚本是否可以在某个点停止并从之前恢复执行?

发布于 2024-12-04 05:32:20 字数 369 浏览 2 评论 0原文

我不知道这是否可以使用 php 来完成,但如果可以的话,这对我来说会很棒,而且 php 非常强大,所以我认为这是可能的,但我只是不知道如何实现。

我知道 php 文件是从上到下执行的,但我想知道是否有一种方法可以在某个时刻停止执行,然后在稍后恢复它,或者是否有一种方法可以到达它的某个部分然后从之前的部分或类似的部分恢复?

我知道如何做到这一点,如果我只是将每个单独的代码块放在一个单独的 php 文件中,这样它就会停止,因为它到达了文件的末尾,如果发生某些事情,它将从另一个文件恢复,所以它几乎同样的事情,但我想制作一个 php 脚本,其行为如下但包含在 1 个 php 文件中。

我不需要代码示例,我只需要在正确的方向上点头,比如一些函数名称或可能有助于实现此目的的东西。

i don't know if this can be done using php but it would be awesome for me if it did and php is pretty powerful so i think it is possible and i just can't figure out how.

i know that a php file is executed from top to bottom but i would like to know if there is a way of stopping the execution at some point to then resume it on a later occasion or is there a way of reaching a certain part of it and then resuming from a part that is before it or something like that?

i know how to make this if i simply put each individual block of code on a separate php file, in this way it will stop because it reached the end of the file and if something happens it will resume from another file so it makes pretty much the same thing, but i would like to make a php script that would behave like this but contained in 1 php file.

i don't need code examples i just need a nod in the right direction, like, some function names or something that could be useful to serve this purpose.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赏烟花じ飞满天 2024-12-11 05:32:20

如果您尝试根据用户输入执行操作,您可能应该:

  1. 将程序构建为 Web 应用程序,以便用户输入的每一位都会向页面生成一个新请求,其中包含输入应执行的代码块,或者...
  2. 使用与 PHP 不同的语言。

假设您正在执行#1,那么拥有一个特定的 GET 参数来指定您在交互中所处的“步骤”并不那么困难 - 例如,假设您有一个 "AB或C<用户输入>D或E”。您可以有一个如下所示的 URL 方案:

/app/code.php - this runs the code block A
/app/code.php?step=B or /app/code.php?step=C - this runs either code block B or C
/app/code.php?step=D or /app/code.php?step=E - this runs either code block D or E

其中所提供的页面上的链接/Javascript/任何内容都允许选择接下来加载哪个页面。您的 PHP 文件将如下所示:

if(!isset($_GET['step'])) {
    // code block A
} elseif($_GET['step'] == 'B') {
    // code block B
} elseif($_GET['step'] == 'C') {
    // code block C
} elseif($_GET['step'] == 'D') {
    // code block C
} elseif($_GET['step'] == 'E') {
    // code block C
} else {
    // error, invalid step specified
}

您还可以简单地将这些步骤拆分为不同的 PHP 文件。哪种方法最好可能取决于不同选项之间共享多少功能以及有多少不同 - 如果共享大量功能,则单个文件可能会更容易;如果大多数不同,单独的文件可能更简单。

If you're trying to do things based on user input, you should probably either:

  1. Structure your program as a web application, such that each bit of user input spawns a new request to the page with the block of code that input should execute, or...
  2. Use a different language than PHP.

Assuming you're doing #1, it's not all that hard to have a particular GET parameter that specifies what "step" you're on in the interaction - for instance, say you have a sequence of "A <user input> B or C <user input> D or E". You could have a URL scheme that looks like this:

/app/code.php - this runs the code block A
/app/code.php?step=B or /app/code.php?step=C - this runs either code block B or C
/app/code.php?step=D or /app/code.php?step=E - this runs either code block D or E

where the links/Javascript/whatever on the served pages allow choosing which page loads next. Your PHP file would look something like this:

if(!isset($_GET['step'])) {
    // code block A
} elseif($_GET['step'] == 'B') {
    // code block B
} elseif($_GET['step'] == 'C') {
    // code block C
} elseif($_GET['step'] == 'D') {
    // code block C
} elseif($_GET['step'] == 'E') {
    // code block C
} else {
    // error, invalid step specified
}

You could also simply split the steps up into different PHP files. Which approach would be best probably depends on how much functionality is shared between the different options, and how much is different - if lots is shared, a single file might be easier; if most is different, separate files are probably simpler.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文