0

I have a PHP function that I'm using to output a standard block of HTML

 <?php function test () { ?>
 echo(" <html>
    <body><h1> HELLO </h1> </body>
</html>
<?php } ?>

but i see the string of HTML and not the page I have see this Is there any way to return HTML in a PHP function? (without building the return value as a string)

Community
  • 1
  • 1
Matteo
  • 1,241
  • 2
  • 21
  • 28

6 Answers6

3

if you use SLIM framework change the content type:

$app->contentType('application/json');

to:

$app->contentType('text/html');

then use render function of slim instance with specific template or simply echo html string

giorgio83
  • 288
  • 2
  • 14
2

you have to do

<?php

function text() {
    echo '<html><body><h1>Hello</h1></body></html>';
}

?>

But in addition this is not the base valid structure of a page. You're missing the <head /> tag.

David Ansermot
  • 6,052
  • 8
  • 47
  • 82
1

Try this:

<?php 
function test () 
    { 
      echo '<html><body><h1> HELLO </h1> </body></html>' ;
    } 

?>
onlyforthis
  • 444
  • 1
  • 5
  • 21
1

Try this:

<?php

function text() {
    return '<html><body><h1>Hello</h1></body></html>';
}

?>

and where you need it just:

<?php echo text(); ?>
Ares Draguna
  • 1,641
  • 2
  • 16
  • 32
0

Try out this:

<?php

    function test()
    {
       $html = '<html>';
       $html .= '<body>';
       $html .= '<h1>Hello</h1>';
       $html .= '</body>';
       $html .= '</html>';

       echo $html;
         //or
       return $html;
    }
?>
Munshi Azhar
  • 307
  • 1
  • 8
0

In PHP you have something called heredoc which lets you write large amounts of text from within PHP, but without the need to constantly escape things. The syntax is <<<EOT [text here] EOT;

So in your case you can have the function return the text like this

function test() {
    return <<<EOT
        <html>
            <body><h1>HELLO</h1></body>
        </html>
    EOT;
 }

Then just call function test to get the contents

echo test();
Jørgen R
  • 10,568
  • 7
  • 42
  • 59