PHP 如果变量不为空,则 echo
我正在使用这个 XML 文件有一些空属性,在这种情况下我想跳过整个属性的列表项。不幸的是,即使其中一个属性为空,我的代码仍然显示所有内容。我确信有一种更简单(且有效)的方法可以做到这一点。这是我的页面和代码:
$file = 'http://www.gostanford.com/*****/data/xml/events/m-baskbl/2010/index.xml';
$xml = simplexml_load_file($file);
foreach($xml as $event_date){
if(isset($event_date->event['vn']) && isset($event_date->event['hn']) && isset($event_date->event['vs']) && isset($event_date->event['hs']))
{
echo '<li>';
echo '<h3>', $event_date->event['vn'], ' vs ', $event_date->event['hn'], '</h3>';
echo '<p><strong>', $event_date->event['local_time'], '</strong></p>';
echo '<h3>', $event_date->event['vs'], ' - ', $event_date->event['hs'], '</h3>';
echo '</li>';
}
}
I'm working with this XML file that has some empty attributes, in which case I want to skip that entire attribute's list item. Unfortunately my code still shows everything, even if one of the attributes are empty. I'm sure there's a simpler (and working) way to do this. Here's my page and code:
$file = 'http://www.gostanford.com/*****/data/xml/events/m-baskbl/2010/index.xml';
$xml = simplexml_load_file($file);
foreach($xml as $event_date){
if(isset($event_date->event['vn']) && isset($event_date->event['hn']) && isset($event_date->event['vs']) && isset($event_date->event['hs']))
{
echo '<li>';
echo '<h3>', $event_date->event['vn'], ' vs ', $event_date->event['hn'], '</h3>';
echo '<p><strong>', $event_date->event['local_time'], '</strong></p>';
echo '<h3>', $event_date->event['vs'], ' - ', $event_date->event['hs'], '</h3>';
echo '</li>';
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将您的
isset
替换为!empty
Replace your
isset
with!empty