如果数组包含任何数据

时间:2011-11-01 16:43:27

标签: php

我在这里有这些位,将为我显示谷歌地图链接。我如何在某种if状态中包装它,以检查$event['where']中是否有任何数据。我不希望在没有数据时显示链接。

<a title="See on Map" target="_blank" href="http://maps.google.com/maps?q=<?php echo $event['where']; ?>">See on map</a>

4 个答案:

答案 0 :(得分:3)

<?php if(!empty($event)){ ?> <a title="See on Map" target="_blank" href="http://maps.google.com/maps?q=<?php echo $event['where']; ?>">See on map</a> <?php } ?>

答案 1 :(得分:1)

试试这个:

<?php echo (!empty($event['where'])) ? $event['where'] : ""; ?>

答案 2 :(得分:1)

如果未设置$event['where'],为空,空白(""),false0,则以下内容不会显示链接 - 我认为是你想要的:

<?php
if (!empty($event['where'])) {
  ?>
  <a title="See on Map" target="_blank" href="http://maps.google.com/maps?q=<?php echo $event['where']; ?>">See on map</a>
  <?php
}
?>

答案 3 :(得分:1)

isset()将确定变量/对象是否已设置且不为空

if(isset($event['where'])){
//...
}

http://php.net/manual/en/function.isset.php