1

I have trouble with the contruction of a working well formmated DOM, through php.

The source-code is diplayed right, but all the dev-tools of Chrome, Firefox and Edge, display the head-tag inside the body-tag. Can you please help me to spot the mistake, beacuse the frontend is now faulty displayed.

it look like this:

enter image description here

php-snippet:

<?php
session_start();

//doctype
echo "<!DOCTYPE HTML>\n";

//html
echo "<html>\n";

//html-head
echo    "<head>\n";
        include "inc/head.html";
echo    "</head>\n";

//html- body start-end
echo    "<body>\n
        some content
        </body>\n</html>\n";
?>

head.html:

<meta http-equiv='content-type' content='text/html; charset=UTF-8' />
<meta name='author' content='MGM'>
<script type='text/javascript' src='http://code.jquery.com/jquery-2.2.0.min.js'></script>
<link rel="shortcut icon" href="media/favicon.png" type="image/png">
<link rel='stylesheet' type='text/css' href='media/desktop.css'>

sourcecode html:

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv='content-type' content='text/html; charset=UTF-8' />
<meta name='author' content='MGM'>
<script type='text/javascript' src='http://code.jquery.com/jquery-2.2.0.min.js'></script>
<link rel="shortcut icon" href="media/favicon.png" type="image/png">
<link rel='stylesheet' type='text/css' href='media/desktop.css'></head>
<body>
some content
        </body>
</html>
Georodin
  • 191
  • 1
  • 2
  • 14
  • When you inspect elements in Chrome's debugging tools you'll get that @chris85 – Jay Blanchard Oct 13 '16 at 19:08
  • @chris85 Its the selected DOM node ID. Short said. – Manuel Mannhardt Oct 13 '16 at 19:08
  • Its chrome specific – Manuel Mannhardt Oct 13 '16 at 19:10
  • Oh, it is a new Chrome (50+) feature. (I'm a 49er and can't go higher) @ManuelMannhardt found it here, https://groups.google.com/forum/#!topic/google-chrome-developer-tools/V_t6vUPjvJg, it is a new Chrome feature(?). – chris85 Oct 13 '16 at 19:14
  • For further information check out this thread: http://stackoverflow.com/questions/36999739/what-does-0-double-equals-dollar-zero-mean-in-chrome-developer-tools – Manuel Mannhardt Oct 13 '16 at 19:15
  • I'm curious as to why you'd want to echo html markup like that, why not just do HTML, then PHP such as `` ? – Funk Forty Niner Oct 13 '16 at 19:19
  • but I still don´t get, why the head always end up in the upper body – Georodin Oct 13 '16 at 19:19
  • @Fred-ii- beacuse I don´t like mixing php and html, but when there is no other option, then I will change my mind about – Georodin Oct 13 '16 at 19:22
  • @Georodin besides what Chrome shows you in dev tools, does the HTML source look/render correctly? It's the source that's the most important here. Or is what you posted (source) the rendered source? If it is, don't worry about the Chrome possible bug. – Funk Forty Niner Oct 13 '16 at 19:26
  • @Fred-ii- the sourcecode html, is the output of the php-snippet are you sure, the source code looks ok for me, but it´s strange, that chrome, firefox and edge, display the head at the wrong place when I open the browser dev-tool for other sites, then the DOM is displayed correctly – Georodin Oct 13 '16 at 19:31
  • @Georodin Well... I can't offer a "why" solution as to why those dev tools throw that; TBH, I don't rely on those when it comes to rendered markup being valid in HTML source. I only use dev tools to catch errors in JS and unclosed tags stuff etc. – Funk Forty Niner Oct 13 '16 at 19:33
  • @Fred-ii- Okay I will leave it, would you recommend the suggestion of stims? Thanks for your efforts! – Georodin Oct 13 '16 at 19:36
  • JS could be altering the DOM. `View source` shows the content from the server, without JS affects. Developer console will take JS changes into account. (Although I don't know why it would ever make that change). My guess would be something isn't closed right and the developer console is incorrectly closing things to try to auto-correct. – chris85 Oct 13 '16 at 19:39
  • @Georodin What stims posted is what I also suggested earlier just minus output buffering. TBH, I don't see the need for output buffering here. A simple include inside php tags will do the same thing. – Funk Forty Niner Oct 13 '16 at 20:42

2 Answers2

0

You can either use file_get_contents() for this

$content = file_get_contents('head.php');
print $content;

Or use the include function but receive its output.

$content = include('head.php');
print $content;

NOTICE Keep in mind, that if you decide to use include for this, it will execute the code inside head.php first, which file_get_contents() wouldnt.

Maybe this also helps you.

Community
  • 1
  • 1
Manuel Mannhardt
  • 2,191
  • 1
  • 17
  • 23
  • Its not you should it this way, its just a way I personally use. And it always works for me. Keep in mind, that if you decide to use include for this, it will execute the code inside head.php first, which file_get_contents() wouldnt. – Manuel Mannhardt Oct 13 '16 at 19:11
0

I would suggest using PHP's output buffer, changing your code to look like this:

<?php
session_start();
ob_start();
?>
    <!DOCTYPE HTML>
    <html>
        <head>
            <?php include "inc/head.html"; ?>
        </head>
        <body>
            some content
        </body>
    </html>

<?php
echo ob_get_clean();
?>
stims
  • 168
  • 4