1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?php function username($user_id = null) { $user_id = ($user_id != null) ? $user_id : current_user(); // if inserted ID to function, else return logged user ID if ( !User::get_user_details($user_id) ) // if user does not exists return $id; // return only ID else { $inner = User::get_user_details($user_id); // if user exist return $inner['username']; // return ID username } } ?>
Refactorings
No refactoring yet !
DevP
May 1, 2008, May 01, 2008 04:49, permalink
Are you sure you want to return the ID if the user does not exist? I'll assume that's what you want...
1 2 3 4 5 6 7 8 9 10 11
<?php function username($user_id = null) { $user_id = is_null($user_id) ? $user_id : current_user(); $user_details = User::get_user_details($user_id); if ($user_details) { return $user_details['username']; } else { return $user_id; } } ?>
Eljay
May 1, 2008, May 01, 2008 21:53, permalink
1 2 3 4 5 6 7
<?php function username($user_id = null) { if(is_null($user_id)) $user_id = current_user(); $user_details = User::get_user_details($user_id); return $user_details ? $user_details['username'] : $user_id; } ?>
WHF
May 16, 2008, May 16, 2008 12:50, permalink
Untested but this is a funny way to do some sort of isset() without the if.
But I'm not sure if it will work with this code.
1 2 3 4 5 6 7
<?php function username($user_id = null) { !is_null($user_id) OR $user_id = current_user(); $user_details = User::get_user_details($user_id); return $user_details ? $user_details['username'] : $user_id; } ?>
<?php