简体   繁体   English

使用条件函数停止PHP while循环

[英]Stop a PHP while loop with function for condition

I'm using the following functions to get the final URL from a series of redirects... 我正在使用以下函数从一系列重定向中获取最终的URL ...

https://stackoverflow.com/a/4102293/1183476 https://stackoverflow.com/a/4102293/1183476

It works great 99.8% of the time. 它的工作效率很高,达到99.8%。 I can't really pinpoint the exception, but I believe it has something to do with the server on the other end generating a new random URL for each visit. 我无法确定异常,但我认为它与另一端的服务器有关,为每次访问生成一个新的随机URL。 Thus, this script gets stuck in an infinite loop. 因此,这个脚本陷入无限循环。

To replicate the issue replace the get_redirect_url function with... 要复制该问题,请将get_redirect_url函数替换为...

function get_redirect_url($url){
    return $url.'x';
}

The Question 问题

How can I set a time or iteration limit? 如何设置时间或迭代限制?

I feel like I've tried everything. 我觉得我已经尝试了一切。 I tried putting a time based condition in the while loop that looks for the next URL, but its not working and I don't know why. 我尝试在while循环中设置一个基于时间的条件,寻找下一个URL,但它不起作用,我不知道为什么。 Like this... 像这样...

function get_all_redirects($url){
    $redirects = array();
    $start = time();
    while ($newurl = get_redirect_url($url) && time()-$start < 10 ){
        if (in_array($newurl, $redirects)){
            break;
        }
        $redirects[] = $newurl;
        $url = $newurl;
    }
    return $redirects;
}

I also tried counting iterations like this... 我也试过计算这样的迭代......

function get_all_redirects($url){
    $redirects = array();
    $i = 0;
    while ($newurl = get_redirect_url($url) && $i < 10 ){
        if (in_array($newurl, $redirects)){
            break;
        }
        $redirects[] = $newurl;
        $url = $newurl;
        $i++;
    }
    return $redirects;
}

The examples above are just 2 of many failed attempts. 上面的例子只是许多失败尝试中的两个。 I'm ready for help. 我准备好了。 Thanks in advance. 提前致谢。

Just scanning (for a second) your code does not show any obvious problems but I would like to make some suggestions. 只扫描(一秒钟)你的代码没有显示任何明显的问题,但我想提出一些建议。

Whenever I see conditionals in control flow statements that use the result of an assignment it always smells fishy (ok, lets say its not my style): 每当我在控制流语句中看到使用赋值结果的条件时,它总是闻起来很腥(好吧,让我们说它不是我的风格):

while ($newurl = get_redirect_url($url) ...

I could bet that by yanking out that assignment/condition or whatever you want to call it, your code will become more readable and maintainable and by some happy chance fix the issue you are seeing. 我敢打赌,通过剔除任务或条件或任何你想要它的代码,你的代码将变得更易读和可维护,并且通过一些快乐的机会解决你所看到的问题。

I am assuming, the loop you are talking of is a real loop, thus the iteration would not work, because of the break here breaks before incrementing if the url is in the array already. 我假设,你所说的循环是一个真正的循环,因此迭代不起作用,因为如果url已经在数组中,则在递增之前中断。

if (in_array($newurl, $redirects)){
    break;
}

Why the timer doesn't work, I dont know. 为什么定时器不起作用,我不知道。 But fixing the incrementing by putting that $i++; 但是通过放置$i++;修复递增$i++; at the top of the loop should at least improve your situation. 在循环的顶部至少应该改善你的情况。

There is a problem in the while : 有一个在一个问题:

while ($newurl = get_redirect_url($url) && time()-$start < 10 )

and some parens should be added around $newurl = get_redirect_url($url) 并且应该在$newurl = get_redirect_url($url)周围添加一些parens

while ( ($newurl = get_redirect_url($url)) && time()-$start < 10 )

instead, since && has higher precedence than = . 相反,因为&&优先级高于= Otherwise $newurl gets the result of the conditional expression get_redirect_url($url) && time()-$start < 10 which is 1 . 否则$newurl得到条件表达式get_redirect_url($url) && time()-$start < 10结果为1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM