How to add PHP code in functions.php wordpress

Perhaps this is what you mean?

<?php
//Insert ads after second paragraph of single post content.
add_filter( 'the_content', 'prefix_insert_post_ads' );
function prefix_insert_post_ads( $content ) {
     
    $ad_code="<div>".sewp_381661_get_advert().'</div>';

    if ( is_single() && ! is_admin() ) {
        return prefix_insert_after_paragraph( $ad_code, 2, $content );
    }
    return $content;
}

// function to return ad markup / logic ##
function sewp_381661_get_advert(){

    $a1 = get_field('fa1', 'option');
    $string = ''; // define return var ##
    if ($a1) {

        $string = '<a href="'.$a1['a1-l'].'" target="_blank" rel="nofollow"><img src="'.esc_url( $a1['a1-b']['url'] ).'"></a>';

    }

    // return string for echo ##
    return $string;

}