<?php if (!empty($box1) && !empty($box2)) { echo ' | content here'; } ?>
<?php if (!empty($box1) && empty($box2)) { echo 'content here'; } ?>
基本上,我想摆脱管道,如果box2是空的。有没有更好的方法来写这个?有没有更优雅的方式来编写这段代码?
<?php if (!empty($box1) && !empty($box2)) { echo ' | content here'; } ?>
<?php if (!empty($box1) && empty($box2)) { echo 'content here'; } ?>
基本上,我想摆脱管道,如果box2是空的。有没有更好的方法来写这个?有没有更优雅的方式来编写这段代码?
<?php if (!empty($box1)) { echo (empty($box2) ? '' : ' | ') . 'content here'; } ?>
谢谢。只是一个侧面的问题。 ($ box2)旁边的问号是什么? – J82 2011-02-12 04:16:52
很难说什么是“最好”的方式将其优雅的书写,而无需一个事物的宏大计划,但至少可以缩短如下:
<?php if(!empty($box1)) { echo (!empty($box2) && ' |') . 'content here'; } ?>
另外,如果你不喜欢&&
风格,你可以使用一个三元操作符:
<?php if(!empty($box1)) { echo (!empty($box2) ? ' |' : '') . 'content here'; } ?>
或者另一个条件。
粗磨,如“最”优雅的方式将沿行采取什么$box1
和$box2
代表定睛一看,然后创建一个视图助手(MVC中的方法):
class SomeModel {
int $box1;
int $box2;
function make_suffix() {
$suffix = '';
if(!empty($this->box1)) {
if(!empty($this->box2)) {
$suffix .= ' | ';
}
$suffix .= 'content here';
}
return $suffix;
}
}
<?php
if (!empty(&box1)) {
if (!empty($box2) {
echo ' | ';
}
echo 'content here';
}
?>
<?php echo !empty($box1) ? (!empty($box2) ? ' | ' : '') . 'content here' : '' ?>
优雅PHP ...哈哈。 – 2011-02-12 04:18:02