我正在尝试使用if语句,这样,如果变量没有设置,它将显示"noimg.jpg"或没有缩略图,但它目前没有这样做,而是显示一个损坏的缩略图。
$feed=new SimplePie_random_sort((;
$feed->set_feed_url(array(
'http://www.thelocal.de/feeds/rss.php',
'http://dailymail.co.uk',
'http://www.exberliner.com/',
'http://www.telegraph.co.uk/news/worldnews/europe/germany/',
));
$feed->set_item_limit(3);
//enable caching
$feed->enable_cache(true);
//provide the caching folder
$feed->set_cache_location('cache');
//set the amount of seconds you want to cache the feed
$feed->set_cache_duration(1800);
//init the process
$feed->init();
//let simplepie handle the content type (atom, RSS...)
$feed->handle_content_type();
<?php foreach ($feed->get_items() as $item): ?>
<?php if ($enclosure = $item->get_enclosure())
{
echo '<img src="' . $enclosure->get_link() . '" class="feed-thumb" />';
}
else echo'<img src="noimg.jpg">';
?>
<?php endforeach; ?>
var_dump的结果:object(SimplePie_Enclosure)#399 (27) { ["bitrate"]=> NULL ["captions"]=> NULL ["categories"]=> NULL ["channels"]=> NULL ["copyright"]=> NULL ["credits"]=> NULL ["description"]=> NULL ["duration"]=> NULL ["expression"]=> NULL ["framerate"]=> NULL ["handler"]=> NULL ["hashes"]=> NULL ["height"]=> NULL ["javascript"]=> NULL ["keywords"]=> NULL ["lang"]=> NULL ["length"]=> NULL ["link"]=> NULL ["medium"]=> NULL ["player"]=> NULL ["ratings"]=> NULL ["restrictions"]=> array(1) { [0]=> object(SimplePie_Restriction)#220 (3) { ["relationship"]=> string(5) "allow" ["type"]=> NULL ["value"]=> string(7) "default" } } ["samplingrate"]=> NULL ["thumbnails"]=> NULL ["title"]=> NULL ["type"]=> NULL ["width"]=> NULL }
我不知道如何根据这些信息修改if语句?
您的if
中有一个任务
if ($enclosure = $item->get_enclosure())
此分配将根据您分配给$enclosure
的值进行评估。如果该值为true,则条件求值为true
。否则,它将评估为false
。问题是,在某些情况下,您希望它的求值为false
,但它的求值却是true
。解决方案是细化条件以评估正确的条件,并确保您不会忘记赋值。将var_dump($enclosure)
放在用花括号表示的块中并重现该问题并不是一个坏主意。倾倒了什么?一个物体?这是怎么回事?以这种方式收集信息并修复条件。