如果Foreach失败,你能做一个Else语句吗


If A Foreach Fails Can You Do an Else Statement?

我有一个Instagram图像检索器的代码,它运行良好,但可能会失败。

<table border="0" width="90%" cellspacing="0" cellpadding="0">
                                        <tr>
                                            <td>
<?php
        function fetch_data($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 20);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }
    $access_token = "xxx.xxx.xxx";
    $display_size = "standard_resolution"; 
    $number_of_images = 7;
    $result = fetch_data("https://api.instagram.com/v1/users/[user]/media/recent/?count={$number_of_images}&access_token={$access_token}");
    $result = json_decode($result);
    $images = array();
    foreach($result->data as $photo)
{
    $images[] = array(
        'url'  => $photo->images->{$display_size}->url,
        'link' => $photo->link,
    );
}
?>
<a href="<?php echo $images[0]['link']; ?>" target="new"><img src="<?php echo $images[0]['url']; ?>" border="0" height="200" width="200" /></td>
                                        </tr>
                                        <tr>
                                            <td><a href="<?php echo $images[1]['link']; ?>" target="new"><img src="<?php echo $images[1]['url']; ?>" border="0" height="200" width="200" /></a></td>
                                        </tr>
                                    </table>
                                    </td>
                                    <td><a href="<?php echo $images[2]['link']; ?>" target="new"><img src="<?php echo $images[2]['url']; ?>" border="0" height="400" width="400" /></a></td>
                                    <td valign=top>
                                    <table border="0" width="100%" cellspacing="0" cellpadding="0">
                                        <tr>
                                            <td><a href="<?php echo $images[3]['link']; ?>" target="new"><img src="<?php echo $images[3]['url']; ?>" border="0" height="200" width="200" /></a></td>
                                        </tr>
                                        <tr>
                                            <td><a href="<?php echo $images[4]['link']; ?>" target="new"><img src="<?php echo $images[4]['url']; ?>" border="0" height="200" width="200" /></a></td>
                                        </tr>
                                    </table>
                                    </td>
                                    <td valign=top>
                                    <table border="0" width="100%" cellspacing="0" cellpadding="0">
                                        <tr>
                                            <td> <a href="<?php echo $images[5]['link']; ?>" target="new"><img src="<?php echo $images[5]['url']; ?>" border="0" height="200" width="200" /></a></td>
                                        </tr>
                                        <tr>
                                            <td><a href="<?php echo $images[6]['link']; ?>" target="new"><img src="<?php echo $images[6]['url']; ?>" border="0" height="200" width="200" /></a></td>
                                        </tr>
                                    </table>
                                    </td>
                                </tr>
                            </table>
                            </td>
                        </tr>
                    </table>
                    </td>
                </tr>
            </table>
            </td>
        </tr>
    </table>

因此,当这个脚本失效时,它说它在第57行失败,第57行是

 foreach($result->data as $photo)

如果这一行失败了,那么显示一条错误消息不会让网站看起来一团糟,还有什么办法吗?

foreach仅适用于arrayobjects,其他所有操作都将生成错误
它不会抛出异常:用try/catch块包围它不会有任何效果

在你的情况下,你必须这样做:

if (is_array  ($result->data) || 
    is_object ($result->data)) {
foreach ($result->data as $photo) {
    $images[] = array ( 'url'  => $photo->images->{$display_size}->url,
                        'link' => $photo->link); 
    // Note that i have removed a comma after $photo->link
}
}else {
    // can not loop ....
}

您可以使用Traversable接口

Traversable接口:使用foreach检测类是否可遍历的接口。

因此,在您的情况下,在foreach之前:

if ($result->data instanceof Traversable) {
    // can use foreach
}