2016-10-13 227 views
0

我有一個工作良好的格式DOM,通過PHP的建設麻煩。包括頭與PHP

右代表源代碼,但Chrome,Firefox和Edge的所有開發工具都在body-tag中顯示head-tag。你能幫我發現錯誤,因爲現在有錯誤顯示的前端。

它看起來像這樣:

enter image description here

的PHP代碼片段:

<?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'> 

源代碼的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> 
+0

當您檢查在Chrome的調試工具元素,你會得到@ chris85 –

+0

@ chris85其選定的DOM節點ID。 Short說。 –

+0

其鉻特定 –

回答

0

您可以使用file_get_contents()函數此

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

或者使用include函數,但接受其輸出。

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

注意 請記住,如果你決定使用包括這一點,它會首先執行其中head.php的代碼,其中的file_get_contents()難道不。

也許this也可以幫助你。

+0

爲什麼要這樣做? –

+0

它不是你應該這樣,它只是我個人使用的一種方式。它總是對我有用。請記住,如果你決定使用include來做這件事,它首先會執行head.php中的代碼,file_get_contents()不會。 –

0

我會建議使用PHP的output buffer,改變你的代碼看起來像這樣:

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