0

I'm trying to create a function 'header' which will print html content (doctype, html, head, body, etc) - but when I'm looking in a site source, all of that stuff is in one line, not in a tree hirearchy...

public function header() {
        print(
                '<!DOCTYPE HTML>'
                . '<html>'
                . '<head>'
                . '<meta charset="utf-8"/>'
                );

And when I'm looking in the web source the output looks like:

<!DOCTYPE HTML><html><head><meta charset="utf-8"/>

I would like it to look more like standard html tree:

<!DOCTYPE HTML>
<html>
    <head>
        <meta charset="utf-8"/>

How can I do that? What are the options ?

EDIT:

Some of You showed me an echo option - it works, but it looks really bad in a php file - like:

            public function header() {
echo "<!DOCTYPE HTML>
<html>
    <head>
        <meta charset='utf-8' />
    </head>
    <body>
    </body>
</html>            
";
2
  • Did you try with echo function? Commented Aug 6, 2016 at 8:49
  • Don't focus too much on code appearance. There will be many other point far more important to think about (security, optimisation, evolution) to achieve your projects. Commented Aug 6, 2016 at 9:07

5 Answers 5

3

The most classic way, using echo :

echo '<!DOCTYPE HTML>
<html>
    <head>
        <meta charset="utf-8"/>

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

3 Comments

but it looks weak as fk in the php file
a bit rough but efficient. I'll try another answer to fit your esthetic request :)
that would be great - please check edit in main post
1

see below methods for printing HTML inside PHP code block

FOR SHORT HTML CONTENTS

echo ' <div class="myClass"> Some Text here. Some text here </p> ';

FOR SHORT HTML CONTENTS WITH PHP variable concatenation

$myName='Optimum';
echo ' <div class="myClass"> My Name is '. $myName .' </p> ';

FOR LONG CONTENT

$html.='';
$phpVariable ='Dummy content'
$html.='<div class="wrapper">'
$html.='<div class="content">';
$html.='<p> My content here'. $phpVariable .' </p>';
$html.='</div>';
$html.='</div>';

echo $html;

According to your scenario

  <?php 
 public function header() { // SOME NECESSARY PHP CODE ?>
 <!DOCTYPE HTML>
     <html>
         <head>
             <meta charset="<?php //echo get_chartset ?>"/>
                 <link rel="stylesheet" type="text/css" href="<?php //echo_css_path ?>">

  } 
  ?>

This will echo/ print clean HTML code in front.

Comments

0

You could introduce tabs when you print:

print("<!DOCTYPE HTML>"
      . "<html>"
      . "\t<head>"
      . "\t\t<meta charset=\"utf-8\"/>"
     );

3 Comments

@user6685192 arg, forgot yo change the quote type - see edited answer.
I've changed it to double quotes, but the result is still weak (there are tabs, but it's still in one line)
for s&g i just tried this... within google chrome and ms edge, view source it is all on a single line.
0

Another approach would be to simply just separate your html template file (Format it however you want) and then just require it with passed data in your function like so

my_view.php

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <h1><?= $heading ?></h1>
</body>
</html>

Your function

function showTemplate($view, array $data) {
    extract($data);
    require $view . '.php';
}

That way you can just call to output your view with data, like this

showTemplate('my_view', [ 'heading' => 'Awesome Page' ]);

This way your template and data would be more organized and pretty.

2 Comments

This does work, but should probably remove the line break between opening " and <!DOCTYPE HTML> as it causes a line break at the very top of the view source.
but it looks really bad in a php file
0

Another way, for doing this :

<?php
function myheader() {
?><!DOCTYPE HTML>
<html>
   <head>
      <meta charset="utf-8"/>

<?php } ?>

I redefined the function name to avoid conflict, (off-topic). I don't know how such a code should be indented ...

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.