What is a more elegant solution to these nested if/elseif statements?

后端 未结 9 1926
野趣味
野趣味 2021-01-12 17:09

I\'m building a website that contains users with user profiles. Many of the fields in the profile are optional.

There is an opportunity for a lot of user-generate

9条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-12 18:03

    I'd go with:

    if( empty($names['display_name']) ) {
        $name = $names['first_name'] . ' ' $names['last_name'];
    else
        $name = $names['display_name'];
    
    $name = trim($name);
    if( empty($name) ) 
        $name = 'user'.$names['id'];
    if( empty($name) ) 
        $name = 'NULL';
    

    This would be the 'core logic'... there will need to be other checks like $names != NULL or something..

提交回复
热议问题