如果一个Foreach失败你可以做一个别的声明吗?

I have this code for a Instagram image retriever, it works well but can fail quite badly.

<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>

So when ever this script dies, it says it fails on line 57 which line 57 is

 foreach($result->data as $photo)

is there anyway to go if this line fails then show so an error message does not make the site look messy?

foreach works only with array or objects, everything else will generates an Error.
It does not throws an exception : surrounding it by try/catch block will have no effects.

In your case you have to do like this :

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 ....
}

You can use Traversable interface

The Traversable interface: Interface to detect if a class is traversable using foreach.

So in your case, before foreach:

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