isset - 用于创建表单的数组中的else

时间:2015-06-23 14:34:50

标签: php arrays

如何在用于创建表单的数组中正确编写isset-> else?

例如:

$this->form[] = array(
    'surname' => (isset($vars['sur_name']) ? $vars['sur_name'] : '')        
);

我想提出else -> 'surname' => $current_user->user_lastname

非常感谢

1 个答案:

答案 0 :(得分:0)

三元条件充当IF ... ELSE条件

$this->form[] = array(
     'surname' => (isset($vars['sur_name']) ? $vars['sur_name'] :  $current_user->user_lastname)      
);

相同
if (isset($vars['sur_name']){
   $this->form[] = array('surname' => $vars['sur_name']);
}
else {
   $this->form[] = array('surname' => $current_user->user_lastname);
}

read about ternary operator here