我有一个回复姓名,地址,电话等的会员目录。我想说明哪些电话号码是家庭,小区或工作,但我不想要家庭,工作或小区的html出现,如果条目没有一个或多个电话号码。有没有办法抑制此信息,只有在该字段中有信息时才会显示字段名称?这是包含三个电话名称的代码。
echo "<ul>\n";
echo "<li>" . $First_Name . " " . $Last_Name . "</li>\n";
echo "<li>" . $Home_Phone . " Home</li>\n";
echo "<li>" . $Cell_Phone . " Cell</li>\n";
echo "<li>" . $Work_Phone . " Work</li>\n";
echo "<li>" . "<a href=mailto:" . $Email . ">" . $Email . "</a></li>\n";
echo "<li>" . $Home_Street . "</li>\n";
echo "<li>" . $Home_City . ", " . $Home_State . " " . $Home_Zip . "</li>\n";
echo "<li>" . $Troop_Role . "</li>\n";
echo "<li>" . $Patrol . "</li>\n";
echo "</ul>";
答案 0 :(得分:2)
if (!empty($Home_Phone))
{
echo "<li>" . $Home_Phone . " Home</li>\n";
}
对其他变量或字段执行相同的检查
答案 1 :(得分:2)
使用ternary运算符进行快速检查
echo "<ul>\n";
echo (empty($Home_Phone)) ? '' : "<li>" . $Home_Phone . " Home</li>\n";