php if语句显示删除缩略图

时间:2015-08-01 23:00:54

标签: php if-statement simplepie

我试图使用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语句?

1 个答案:

答案 0 :(得分:2)

您的if

内有作业
if ($enclosure = $item->get_enclosure())

此分配将评估为您分配给$enclosure的值。如果该值为true,则条件计算为true。否则,它将评估为false。问题是,在某些情况下,您希望它评估为false,但评估为true。解决方案是优化条件以评估正确的条件,并确保您不会忘记值赋值。将var_dump($enclosure)放在以大括号括起来的块中并重现问题并不是一个坏主意。什么是倾销?一个东西?这怎么样?获取您以这种方式收集的信息并修复条件。

相关问题