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)

6 Answers 6

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

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.

1
  • This method not found I use SLIM and return the string and not the page.
    – Matteo
    Oct 28, 2014 at 9:39
1

Try this:

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

?>
1
  • not found i use Slim and see only the string of html
    – Matteo
    Oct 24, 2014 at 10:10
1

Try this:

<?php

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

?>

and where you need it just:

<?php echo text(); ?>
0

Try out this:

<?php

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

       echo $html;
         //or
       return $html;
    }
?>
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();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.