PHP如何使用$ var [' table']否则如果[' table']为空以显示空白

时间:2016-08-01 18:47:07

标签: php

我有一个基于查询返回结果的数据库:

<?php foreach ($data as $key=> $item) { ?>
<tr>
<td class="text-left"><?php echo ucwords(strtolower($item['biz_name'])); ?></td>
<td class="text-left"><?php echo '<a href="http://maps.google.com/?q='. $item['loc_LAT_centroid'] .','. $item['loc_LONG_centroid'] .'">'.ucwords(strtolower($item['e_address'])).'</a>'; ?></td>
<td class="text-left"><?php echo ucwords(strtolower($item['e_city'])); ?></td>
<td class="text-center"><?php echo strtoupper($item['e_state']); ?></td>
<td class="text-center"><?php echo $item['e_postal']; ?></td>
<td class="text-center"><?php echo $item['biz_phone']; ?></td>
<?php if (empty($item['web_url'])) {
    <td class="text-center"><?php echo ''; ?></td>
}
else {
    <td class="text-center"><?php echo '<a href="'. $item['web_url'] .'" >'."View Site".'</a>'; ?></td>
} ?>

我显然做错了,但到目前为止,这就是我拼凑的内容。我尝试做的是,如果值为null,则web_url表字段显示为空白,否则显示&#34;查看站点&#34;如果它包含一个值。我对php很新,所以感谢任何帮助。

我现在在这一行得到一个解析错误:

<?php if (empty($item['web_url'])) {

1 个答案:

答案 0 :(得分:0)

在这种情况下你应该使用ternary operator

<td class="text-center">
    <?php echo (isset($item['web_url']) && !empty($item['web_url'])) ? ('<a href="'. $item['web_url'] .'" >'."View Site".'</a>') : ''; ?>
</td>