如何创建考虑空变量的字符串?
我正在尝试为页面构建占位符元描述,以防用户未在 CMS 中包含描述。
我从以下代码开始,但是如果任何其他变量也为空,例如 $phone
、$location['zip']
和很快。
<?php
if (!empty($description)) {
echo '<meta name="description" content="' .$description . '">';
}
else {
// Should return: Apple is a business located in Palo Alto, 95014. Call 408.996.1010...
$description = $name . ' is a ' . strtolower($category) . ' located in ' . $location['city'] . ', ';
$description .= $location['zip'] . '. Call ' . $phone . ' for more details today.';
echo '<meta name="description" content="' . $description . '">';
} ?>
以这种方式构建描述的最有效方法是什么?目前我只能想到嵌套的 if 语句,这听起来很混乱,我确信必须有一个干净的方法来做到这一点。
I'm trying to build a placeholder meta description for a page, in case the user hasn't included a description in the CMS.
I have started with the following code, but of course it fails if any of the other variables are empty too, such as $phone
, $location['zip']
and so on.
<?php
if (!empty($description)) {
echo '<meta name="description" content="' .$description . '">';
}
else {
// Should return: Apple is a business located in Palo Alto, 95014. Call 408.996.1010...
$description = $name . ' is a ' . strtolower($category) . ' located in ' . $location['city'] . ', ';
$description .= $location['zip'] . '. Call ' . $phone . ' for more details today.';
echo '<meta name="description" content="' . $description . '">';
} ?>
What's the most efficient way to build a description in this way? Currently I can only think of nested if statements which sounds messy and I'm sure there must be a clean way to do this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
添加一个函数来检查值是否已设置?
IE
add a function to check if value is set?
i.e.
由于描述是根据输入而变化的,因此将其放入自己的函数或类中进行封装:
完成后,
build_description
中的具体实现可以包含很多复杂的语句,而程序的其余部分可以像处理简单的事情一样处理它。但是,这并没有回答如何在该函数内对其进行编码。但由于该函数的输出数据在很大程度上取决于该函数的输入,因此您只能处理参数确实施加的所有方面。
As the description is something that varies based on the input, put it into a function or class of it's own to encapsulate it:
that done, the concrete implementation within
build_description
can contain a lot of complex statements, while the rest of the program can deal with it as if it is something simple.However, this does not answer how you could code it inside that function. But as the data of the output of that function heavily depends on the input of that function, you can only deal with all aspects the arguments do impose.
定义的其他变量不应该是字符串,而应该是对象的一部分,例如...描述。
在这种情况下,调用 Description->isEmpty() 会更容易,如果这些变量之一为空,则返回 true。
如果您坚持这种配置,您仍然可以创建一个数组:
$myArray=数组($name, $category,...);
并检查循环或可能返回 in_array('',$myArray)
The other variables defined shouldn't be string but part of an object such as... Description.
In this case, it would be easier, calling a Description->isEmpty() that returns true if one of those variables are empty.
If you are stuck with this configuration, you still can make an array:
$myArray=array($name, $category,...);
and check in a loop or maybe the return of in_array('',$myArray)