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-generated content, and so I need to display the author of this content in many different locations of the site (comments, posts, etc.). In the user's profile, he is able to (optionally) fill out his "first name", his "last name", and a "display name".
To display the author, I wrote a helper method that looks through a provided array of these fields and returns the most appropriate name for the user, in this order of preference:
display_name
, this will be displayed.first_name
and last_name
, but no display_name
, it will display both namesfirst_name
, it will display first_name
.last_name
, it will display last_name
.user123
NULL
The method works great, but it's ugly. There must be a way to beautify this with an alternative to nested if/else statements.
public function nameify($names = NULL) {
$name = '';
if (!empty($names)) {
if (!empty($names['display_name'])) {
$name = $names['display_name'];
} elseif (!empty($names['first_name'])) {
$name = $names['first_name'];
if (!empty($names['last_name'])) {
$name .= ' ' . $names['last_name'];
}
} elseif (!empty($names['last_name'])) {
$name = $names['last_name'];
}
if (empty($name) && !empty($names['id'])) {
$name = 'user' . $names['id'];
} else {
$name = 'NULL';
}
} else {
$name = 'NULL';
}
return $name;
}
Alternatives to nested IF in Excel To test multiple conditions and return different values based on the results of those tests, you can use the CHOOSE function instead of nested IFs.
A function should do only one thing, if you have nested if like this your function is certainly doing more than one thing. So that means, each if/else if/else block should then be in its own function. This solved the nested if problem. And the extracted function should be given a descriptive name of what it does.
Remarks. While Excel will allow you to nest up to 64 different IF functions, it's not at all advisable to do so.
public function nameify($names = NULL) {
if ($names) {
if (!empty($names['display_name'])) {
return $names['display_name'];
}
if (!empty($names['first_name'])) {
$name = $names['first_name'];
}
if (!empty($names['last_name'])) {
$name .= ' ' . $names['last_name'];
}
if (empty($name) && !empty($names['id'])) {
$name = 'user' . $names['id'];
}
}
return $name ? ltrim($name) : 'NULL';
}
Set the default first, and return that if nothing else matches. Then since we always want to return the display name if we have it do just that.
EDIT: Tweak to prevent returning "NULL "
Using ternary conditions we can shorten and beautify the code:
public function nameify($names = NULL) {
$name = 'NULL';
if (!empty($names)) {
$name = ($names['display_name']) ? $names['display_name'] : trim($names['first_name']." ".$names['last_name']);
if(!$name) $name = ($names['id'] > 0) ? 'user'.$names['id'] : 'NULL';
}
return $name;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With