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

Sign up to request clarification or add additional context in comments.

Comments

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 Comment

This method not found I use SLIM and return the string and not the page.
1

Try this:

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

?>

1 Comment

not found i use Slim and see only the string of html
1

Try this:

<?php

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

?>

and where you need it just:

<?php echo text(); ?>

Comments

0

Try out this:

<?php

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

       echo $html;
         //or
       return $html;
    }
?>

Comments

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();

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.