如果0
为空
nb_impressions
foreach ($content as $row) {
$impressions = $row['nb_impressions'];
print("<td'>$impressions</td>");
}
该值来自HTTP API
但是,如果该值为空则显示为空,我如何才能提供这些空值0
答案 0 :(得分:1)
使用三元运算符检查为
(isset($row['nb_impressions']) && $row['nb_impressions'])
并设置默认0
foreach ($content as $row) {
$impressions = (isset($row['nb_impressions']) && $row['nb_impressions']) ? $row['nb_impressions'] : 0;
print("<td'>$impressions</td>");
}
发表评论后,您可以将其合并为一个检查功能
function isExist($var,$default=''){
if (isset($var) && $var) {
return $var;
}
return $default;
}
并像这样使用
$impressions = isExist($row['nb_impressions'], 0);
$clicks= isExist($row['nb_clicks'], 0);
$visits = isExist($row['nb_visits'], 0);
答案 1 :(得分:1)
您只需添加if
声明
foreach ($content as $row) {
$impressions = $row['nb_impressions'];
if(isset($impressions)){
print("<td'>$impressions</td>");
}else{
print("<td>0</td>");
}
}
答案 2 :(得分:1)
使用php empty()函数检查
foreach ($content as $row) {
if(!empty($row['nb_impressions'])){
$impressions = $row['nb_impressions'];
}
else{
$impressions = 0;
}
print("<td'>$impressions</td>");
}
简单直接。