如果没有名字,显示用户名

If no name, show username

正在为旧网站做一些维护工作。如果输入,我想显示用户的full name区域,否则显示用户的username。我不知道为什么下面的代码不起作用,我没有收到任何错误消息。

    <?php
global $user;
$uid = user_load($user->uid);
$myprofile = 'main';
$profile = profile2_load_by_user($uid, $myprofile);

$display_name = $profile-> field_main_first_name['und'][0]['value'] &" "& $profile-> field_main_last_name['und'][0]['value'];

if(empty($display_name)){
    echo $display_name = $user->uid;
}

echo $display_name
?>

以下对我有用。

    <?php
global $user;
$uid = user_load($user->uid);
$myprofile = 'main';
$profile = profile2_load_by_user($uid, $myprofile);

$first_name = $profile-> field_main_first_name['und'][0]['value'];
$last_name = $profile-> field_main_last_name['und'][0]['value'];

if (empty($first_name)) {
    $display_name = $user->name;
  } else {
    $display_name = $first_name." ".$last_name;
  };
?>