82

I have a PHP function that I'm using to output a standard block of HTML. It currently looks like this:

<?php function TestBlockHTML ($replStr) { ?>
    <html>
    <body><h1> <?php echo ($replStr) ?> </h1>
    </html>
<?php } ?>

I want to return (rather than echo) the HTML inside the function. Is there any way to do this without building up the HTML (above) in a string?

j08691
  • 204,283
  • 31
  • 260
  • 272
seanyboy
  • 5,623
  • 7
  • 43
  • 56

8 Answers8

115

You can use a heredoc, which supports variable interpolation, making it look fairly neat:

function TestBlockHTML ($replStr) {
return <<<HTML
    <html>
    <body><h1>{$replStr}</h1>
    </body>
    </html>
HTML;
}

Pay close attention to the warning in the manual though - the closing line must not contain any whitespace, so can't be indented.

Community
  • 1
  • 1
Paul Dixon
  • 295,876
  • 54
  • 310
  • 348
98

Yes, there is: you can capture the echoed text using ob_start:

<?php function TestBlockHTML($replStr) {
    ob_start(); ?>
    <html>
    <body><h1><?php echo($replStr) ?></h1>
    </html>
<?php
    return ob_get_clean();
} ?>
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • 3
    Exactly, this way syntax is highlighted and key words are colored as they would being normal HTML, other than being content in a string. Definitely a better answer for maintainability, which should always come first – Brian Leishman May 08 '15 at 14:52
  • Why would you use the output buffer when this seems to work fine? `

    Lorem ipsum dolar

    } ?> `
    – Joren Jun 15 '15 at 21:34
  • 1
    @Joren Because that's explicitly what the OP doesn't want. – Konrad Rudolph Jun 15 '15 at 23:18
  • 1
    Ok, so if you wanted to echo it, my example would be the way to go? – Joren Jun 17 '15 at 00:23
  • @Joren Yes, essentially. – Konrad Rudolph Jun 17 '15 at 20:10
  • This is perfect. You can even include echo statements after ob_start and it is included in ob_get_clean()'s output. I am writing modules for a third-party script, and it forces you to put all of the output for the page into a get() function. This is a lifesaver. – Scott M. Stolz Oct 11 '22 at 19:28
19

This may be a sketchy solution, and I'd appreciate anybody pointing out whether this is a bad idea, since it's not a standard use of functions. I've had some success getting HTML out of a PHP function without building the return value as a string with the following:

function noStrings() {
    echo ''?>
        <div>[Whatever HTML you want]</div>
    <?php;
}

The just 'call' the function:

noStrings();

And it will output:

<div>[Whatever HTML you want]</div>

Using this method, you can also define PHP variables within the function and echo them out inside the HTML.

Lorien Brune
  • 830
  • 10
  • 16
9

Create a template file and use a template engine to read/update the file. It will increase your code's maintainability in the future as well as separate display from logic.

An example using Smarty:

Template File

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head><title>{$title}</title></head>
<body>{$string}</body>
</html>

Code

function TestBlockHTML(){
  $smarty = new Smarty();
  $smarty->assign('title', 'My Title');
  $smarty->assign('string', $replStr);
  return $smarty->render('template.tpl');
}
Rob
  • 7,377
  • 7
  • 36
  • 38
5

Another way to do is is to use file_get_contents() and have a template HTML page

TEMPLATE PAGE

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head><title>$title</title></head>
<body>$content</body>
</html>

PHP Function

function YOURFUNCTIONNAME($url){

$html_string = file_get_contents($url);
return $html_string;

}
Cayde 6
  • 617
  • 6
  • 19
  • 1
    This is the closest to what I'm looking for. @Cayde 6, do you think theres a way to have a single file 'template.php', containing both the php function and html? So then anywhere in my site, I just 'require_once('template.php')', then call echo the function wherever I need that html to appear. In the YOURFUNCTIONNAME($url), would the $url would need to be itself? How can we do that? Thanks – phpN00b Mar 26 '18 at 00:44
0

Or you can just use this:

<?
function TestHtml() { 
# PUT HERE YOU PHP CODE
?>
<!-- HTML HERE -->

<? } ?>

to get content from this function , use this :

<?= file_get_contents(TestHtml()); ?>

That's it :)

0

If you don't want to have to rely on a third party tool you can use this technique:

function TestBlockHTML($replStr){
  $template = 
   '<html>
     <body>
       <h1>$str</h1>
     </body>
   </html>';
 return strtr($template, array( '$str' => $replStr));
}
marrion luaka
  • 139
  • 2
  • 6
0

Template File

<h1>{title}</h1>
<div>{username}</div>

PHP

if (($text = file_get_contents("file.html")) === false) {
    $text = "";
}

$text = str_replace("{title}", "Title Here", $text);
$text = str_replace("{username}", "Username Here", $text);

then you can echo $text as string

Optimaz Prime
  • 857
  • 10
  • 11