测试Foreach是否成功

时间:2013-02-20 03:42:10

标签: php

我需要一种方法来检测' foreach '是否不成功。如果不成功,则重复当前“其他”中的相同错误消息

<?php

    if(file_exists('redirects.xml')) {
        $xml = simplexml_load_file('redirects.xml');
        if(isset($_GET['r']) && $_GET['r'] != '' && !is_array($_GET['r'])) {
            foreach($xml->short as $shorts) {
                if($shorts->name == $_GET['r']) {
                    header('Location: '.$shorts->url);
                    break;
                }
            }
        }
        else {
            header("refresh:2;url=http://www.wlatw.co/");
            echo '<div align="center" style="font-weight: bolder; font-size: 24px;">Malformed URL</div><div align="center" style="font-weight: bolder; font-size: 16px;">Redirecting...</div>';
        }
    }
?>

2 个答案:

答案 0 :(得分:3)

在开始循环之前创建一个标志; 在失败时将其设置为不成功。

<?php

    if(file_exists('redirects.xml')) {
        $xml = simplexml_load_file('redirects.xml');
        if(isset($_GET['r']) && $_GET['r'] != '' && !is_array($_GET['r'])) {
            $success = false; // set the flag
            foreach($xml->short as $shorts) {
                if($shorts->name == $_GET['r']) {
                    header('Location: '.$shorts->url);
                    $success = true;
                    break;
                }
            }

            if ($success) { // do what you want when not success ful.
                header("refresh:2;url=http://www.wlatw.co/");
                echo '<div align="center" style="font-weight: bolder; font-size: 24px;">Malformed URL</div><div align="center" style="font-weight: bolder; font-size: 16px;">Redirecting...</div>';
            }
        }
        else {
            header("refresh:2;url=http://www.wlatw.co/");
            echo '<div align="center" style="font-weight: bolder; font-size: 24px;">Malformed URL</div><div align="center" style="font-weight: bolder; font-size: 16px;">Redirecting...</div>';
        }
    }
?>

但是看看你的代码,你可以在设置标题后退出:

foreach($xml->short as $shorts) {
    if($shorts->name == $_GET['r']) {
        header('Location: '.$shorts->url);
        exit;
        break;
    }
}

注意:正如@Sverri M. Olsen所说,您应该在设置Location标头后停止脚本,无论是使用die,exit还是其他任何机制。

答案 1 :(得分:2)

您需要添加一个变量来跟踪循环的状态。

<?php

if(file_exists('redirects.xml')) {
    $xml = simplexml_load_file('redirects.xml');
    if(isset($_GET['r']) && $_GET['r'] != '' && !is_array($_GET['r'])) {
        $url_is_malformed = false;
        foreach($xml->short as $shorts) {
            if($shorts->name == $_GET['r']) {
                header('Location: '.$shorts->url);
                break;
            }
        }
        $url_is_malformed = true;
    }
    else {
        $file_doesnt_exist = true;
    }

    if( $file_doesnt_exist || $url_is_malformed )
    {
        header("refresh:2;url=http://www.wlatw.co/");
        echo '<div align="center" style="font-weight: bolder; font-size: 24px;">Malformed URL</div><div align="center" style="font-weight: bolder; font-size: 16px;">Redirecting...</div>';
    }
}

&GT;