You can see in the code that if $user is empty, the id will be taken from the logging information (more specific, $USER->id). So you should use your condition after the id is set.
Write your code right before this line
$REMOTE_ADDR = getremoteaddr();
You'll need to use $userid and not $user
if ($userid == 3 || $userid == 4 || $userid == 5)
(to my knowledge, the if statement is the fastest approach)