Show referenced nodes as links if they have content in a field
This can be done in Views with no PHP or templating. 

Add 2 title fields, one that is a link to the content and one that isnt. 

Exclude both fields from display.

Add a body field and rewrite the output of the field. Use a replacement pattern thats the node title which is a link.

Under no results behavior add the token thats the node title which isnt a link. Make sure 'Hide rewriting if empty' is not checked.