如何在PHP函数中包含HTML,以后再渲染?

I need to include html code inside a php function,after to use the function in another html code. The HTML code should be in another file, i want to call that code in specified row in another file.

HTML code

<span>
<?php
$register_date = new Moment\Moment($u->get("date"), date_default_timezone_get());
$register_date->setTimezone($AuthUser->get("preferences.timezone"));
$format = $AuthUser->get("preferences.dateformat");
if (!$format) {
$format = "Y-m-d";
}
?>
 <?= __("Register date: %s", $register_date->format($format)) ?>
</span>

The function

function thefunction (){
//the html code here
}

The code i want to include the function

<?php if ($u->get("expire_date") < "2050"): ?>
<div class="meta">

<?php ?>
function thefunction ()//The function
?>

</div>
<?php endif ?>

Normal code without the function

<?php if ($u->get("expire_date") < "2050"): ?>
<div class="meta">

<span>
<?php
$register_date = new Moment\Moment($u->get("date"), date_default_timezone_get());
$register_date->setTimezone($AuthUser->get("preferences.timezone"));
$format = $AuthUser->get("preferences.dateformat");
if (!$format) {
$format = "Y-m-d";
}
?>
 <?= __("Register date: %s", $register_date->format($format)) ?>
</span>

</div>
<?php endif ?>

One (more elegant) approach, all in php, would be:

<?php
    //php code
    function sayHello (){
        echo '<p>Hello</p>';
    }
    sayHello();
    sayHello();
?>

Another approach would be:

<?php
//php code
function sayHello(){ //leave that bracket open ?>

    <!--this is html but you're still inside the function. this won't be printed until you call the function-->
    <p>Hello</p>

<?php } //function ends here ?>

Then, in any point:

<?php sayHello(); ?>

and your html will be printed