Include files and get multiple repetitions of html and body tags can give problem?

0

Assuming I have a file named home.php and it is has 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>

I also have, for example, a file to include the files by header , ì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 then the structure of the files ( home.php , contato.php , pedidos.php and so on ...) 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 to inspect the source code of the page, we see the following way (with several repetitions of the tags <html> and <body> , this can 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>
    
asked by anonymous 20.03.2018 / 02:52

1 answer

1

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

In addition to being able to cause some damage to the page performance, as the browser will have more code to load and make the correction, I do not see much sense developing a page this way.

Ideally, include should contain only the code you want to insert into the page, such as 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>
    
20.03.2018 / 11:35