如果从用户端接收到输入并继续循环,我如何打破连续循环


How can i break my continuous loop, if a input is received from user side and continue with the loop

如果从用户端接收输入并继续循环,我如何打破连续循环

<?php
{
   //first part of code
}
//endless loop, based on the first part, until an onclick method sends an ajax request?
while (1)
{
   ...
   if ($variable_is_set_by_button_press) break;
}
{
   //last part of code giving me some feedback about execution time and such
}
?>

Thanks in advance

这些语言结构是专门为循环设计的,也许它会有所帮助:

break -跳出循环,

continue -进入下一个循环过程

我不知道您为什么要这样做,但是您可以通过使用数据库变量控制循环来实现:

while (checkDatabaseVariable() == true) {
// DO STUFF
}

你可以在另一个脚本中设置这个变量为false,你的PHP循环应该会检测到这个变化并跳出循环。

你可以尝试使用APC。假设你的循环运行在script1.php,与ajax:

while(1){
  if(apc_fetch($user . '_stop'))
    break;
}

按下按钮时,触发对script2.php的另一个ajax请求:

apc_store($user . '_stop', 1);

$user可以是会话ID,也可以是当前用户IP、user -agent等的哈希值。

@ tyrus 100%正确。PHP是在服务器端执行的,而不是在客户端(浏览器)。

因此,如果您想对用户的输入作出反应,您应该使用AJAX并在发生这种情况时向服务器执行异步请求。