How to link featured images in single.php for only non-logged-in users?

This code that you are referencing needs to be placed somewhere within the loop in your single.php file. As this code is written, you will not see it make any sort of output to the screen since it is just saving $image and $link to variables.

You will need to do something like this to output the image:

<?php

$image = get_the_post_thumbnail_url( $post->ID, 'large' );
$link  = is_user_logged_in() ? $image : 'https://example.com/';

?>

<a href="https://wordpress.stackexchange.com/questions/313425/<?php echo $link; ?>"><img src="<?php echo $image; ?>"></a>