如果PHP / YII中不为空,则显示字段

问题描述:

I used to code with Groovy ... I found that this 'feature' (no idea what they called it) so fun n nice (I heard they implemented this on C# too).

for example ... I want to display person neighbour name

I just type

println person?.neigbour?.name ; 

it means if the neighbour is empty / blank .. it didn't display anything .

how to do this in php 5/yii?

example:

instead of typing long codes like '/>

Would it be better to type like '/>

我曾经用Groovy编码...我发现这个'功能'(不知道他们叫什么) 非常好玩(我听说他们也在C#上实现了这一点)。 p>

例如...我想显示人物邻居名称 p>

我只需输入 p>

  println person?.neigbour?.name;  
  code>  pre> 
 
 

这意味着如果邻居为空/空白..它没有显示任何内容。 p>

如何执行此操作 在PHP 5 / yii? p>

示例: p>

而不是输入长代码,例如 '/> p>

会不会 最好输入类似 '/> p> div>

In the first place I think this is no Yii issue, but simply PHP. Assuming, that you use Yii with nice models, there it would go sth like this:

if($person && $person->neighbour && !empty($person->neighbour->name))  {
   echo $person->neighbour->name;
}

a shortcut for this may be (not so nice):

echo $person ? ($person->neighbour ? ($person->neighbour->name ? $person->neighbour->name : "" ) : ""  ) : "";

use empty() check:

if(!empty($variable))
{
//show fields here
}