循环,直到请求-响应消息等于;“真的”;在php中
我正在尝试制作一个php脚本,它将生成一个循环,以获取我的站点/服务器的内容,如果文本响应是“false”,那么它将执行相同的操作,基本上将循环,直到站点的文本响应返回“true”。 这就是我所尝试的:循环,直到请求-响应消息等于;“真的”;在php中,php,loops,response,Php,Loops,Response,我正在尝试制作一个php脚本,它将生成一个循环,以获取我的站点/服务器的内容,如果文本响应是“false”,那么它将执行相同的操作,基本上将循环,直到站点的文本响应返回“true”。 这就是我所尝试的: $getcontents = file_get_contents("http://example.com/script.php"); // it will echo false if (strpos($getcontents , 'false')) { $
$getcontents = file_get_contents("http://example.com/script.php"); // it will echo false
if (strpos($getcontents , 'false')) {
$getcontents = file_get_contents("http://example.com/script.php");
else if (strpos($getcontents , 'false')) {
$getcontents = file_get_contents("http://example.com/script.php");
}
else if (strpos($getcontents , 'true')) {
echo "finished".;
}
我不确定这是否是正确的方式,甚至不确定这是否可能,如果我没有很好地解释自己,我会提前道歉。谢谢大家的关注 您可以使用常规的
while
循环
$getcontents = 'false'; //set value to allow loop to start
while(strpos($getcontents , 'false') !== false) {
$getcontents = file_get_contents("http://example.com/script.php");
}
echo "finished";
这将循环,直到$getcontents
不包含false
您也可以使用这样的递归函数
function check_for_false() {
$getcontents = file_get_contents("http://example.com/script.php");
if(strpos($getcontents , 'false') !== false) {
check_for_false();
} else if(strpos($getcontents , 'true') !== false) {
echo "finished";
} else {
echo "response didn't contain \"true\" or \"false\"";
}
}
此函数应该一直调用自己,直到
$getcontents
包含单词true
,并且不包含false
您的第一行缺少双引号。我刚刚注意到,谢谢!别忘了strpos()
可以返回0(falsy)。是的,但我希望循环是无限的,直到响应为真…@Avery这正是它的作用。@Avery没问题。如果它不能像你期望的那样工作,请告诉我。