抑制空行中的 html 回显

发布于 2024-11-02 08:34:01 字数 782 浏览 2 评论 0原文

我有一个成员目录,其中包含姓名、地址、电话等。我想指出哪些电话号码是家庭、手机或工作电话号码,但如果条目不存在,我不希望显示家庭、工作或手机的 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>";

I have a members directory that echoes name, address, phones, etc. I want to indicate which phone numbers are home, cell or work, but I don't want the html of home, work or cell to appear if the entry does not have one or more of those phone numbers. Is there a way to suppress this information and have the field name only appear if there is information in that field? Here is the code with the three phone names in it.

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>";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不醒的梦 2024-11-09 08:34:01

使用 三元 运算符进行快速检查

echo "<ul>\n"; 
echo (empty($Home_Phone)) ? '' : "<li>" . $Home_Phone . " Home</li>\n";

Use a ternary operator for a quick check

echo "<ul>\n"; 
echo (empty($Home_Phone)) ? '' : "<li>" . $Home_Phone . " Home</li>\n";
夏の忆 2024-11-09 08:34:01

在回显之前检查它们是否为空,不要使用 if 构造 回显它们和 empty

if (!empty($Home_Phone))
{
   echo "<li>" . $Home_Phone . " Home</li>\n";
}

对其他变量或字段进行相同的检查

check them before echoing if they are empty do not echo them with if construct and empty

if (!empty($Home_Phone))
{
   echo "<li>" . $Home_Phone . " Home</li>\n";
}

Do the same check for other variables or fields

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文