Can including files and repeating <html> and <body> tags cause problems?

Asked

Viewed 46 times

0

Assuming I have a file called home.php and he’s got the following codes:

<html>
<head>
    <p>Head do home.php</p>
</head>
<body>
    <span>Body do home.php</span>
</body>
</html>

Then I also have the file menu.php:

<html>
<head>
    <p>Head do menu.php</p>
</head>
<body>
    <div>
        <span>Body do menu.php</span>
    </div>
</body>
</html>

And I also have for example a file to include the files by header, the ìncludes.php for example:

<html>
<head>
    <script type="text/javascript"></script>
    <script type="text/javascript"></script>
    <link rel="stylesheet" type="text/css">
    <link rel="stylesheet" type="text/css" href="">
</head>
<body>
</body>
</html>

And there’s the file structure(home.php, contato.php, pedidos.php and so it goes...) would look like this:

<?php
include_once 'includes.php';
include_once 'menu.php';
?>
<html>
<head>
    <p>Head do home.php/contato.php e por ai vai...</p>
</head>
<body>
    <span>Body do home.php/contato.php e por ai vai...</span>
</body>
</html>

Everything works correctly, however, if we are going to inspect the code of the page, we see as follows (with several repetitions of the tags <html> and <body>, this may give problem or the browsers correct?):

 <html>
<head>
    <script type="text/javascript"></script>
    <script type="text/javascript"></script>
    <link rel="stylesheet" type="text/css">
    <link rel="stylesheet" type="text/css">
</head>
<body>
</body>
</html><html>
<head>
    <p>Head do menu.php</p>
</head>
<body>
    <div>
        <span>Body do menu.php</span>
    </div>
</body>
</html><html>
<head>
    <p>Head do home.php/contato.php e por ai vai...</p>
</head>
<body>
    <span>Body do home.php/contato.php e por ai vai...</span>
</body>
</html>
  • Working works (maybe not in IE), but working is not always correct or the best way

1 answer

1


More modern and pre-modern browsers (IE 8+) even fix, but this is a bad and unassailable practice. A include should contain only the code that interests that section of the page, and not a complete page structure, with <html>, <head> and <body>.

Besides being able to cause some damage in the performance of the page, because the browser will have more code to load and make the correction, I do not see much sense to develop a page this way.

The ideal is that the include contains only the code you want to insert on the page, for example the page index.php:

<html>
<?php
include_once 'head.php';

     // o head.php teria o código do <head></head>
     // com as meta-tags, scripts e o que for
     // necessário incluir neste bloco

?>
<body>
<?php
include_once 'menu.php';

     // o menu.php teria o HTML do menu, ex.:
     // <nav> conteúdo do menu </nav>
?>

....
CONTEÚDO DA HOME
....

<?php
include_once 'footer.php';
?>
</body>
</html>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.