phphtmlload

Div is being placed wrongly when browser loads the php file


I have a php file that pulls some information from database and then displays inside a template html with "echo". But some normal html is there. However, whenever I load the page on the browser (with a server) the html that isn't a part of echo gets inside the div above it.

so basically:

<body>
  <div class="container">
    <div class="parent">
       <div class="child">
         <?php echo '<div>html stuff</div>' ?>
       </div>
    </div>
  </div>
  <div class="normal html">
    <ul>
      <li>Random stuff</li>
    </ul>
  </div>
</body>

when loaded in the browser becomes:

<body>
  <div class="container">
    <div class="parent">
       <div class="child">
         <?php echo '<div>html stuff</div>' ?>
       </div>
       <div class="normal html">
         <ul>
           <li>Random stuff</li>
         </ul>
       </div>
    </div>
  </div>
</body>

This breaks the layout and css I've come up with, so I wanted to know what's happening. This is just an example as the real code is much bigger, I do queries and mess with data. Most likely it's some dumb error, but nothing's fired up in logs. Also, im not using any ajax, just pure php.

edit: for better clarification I cleared the example code and I will add more information: I am echoing 2 div inside an existing html div, and these TWO DIVs being echoed has other html elements inside it. For better understanding, the existing HTML DIV will be called "div 1", and the echoed divs will be called "div 2" and "div 3". Outside of "div 1", there is another div that is not being outputted by PHP, it's already being placed as normal HTML, along with a link tag to a JavaScript file. This div that's not being outputted by PHP and this link tag to a JavaScript file, after the browser loads, is ending up inside "div 1", when they are not supposed to be there.

This edit is a written example of what is happening, but the code before it is a visual example of what's happening inside the code.


Solution

  • Make sure you close your apostrophe:

    <?php echo '<div>html stuff</div>'; ?>
    

    Also, make sure that the html stuff that you returned is proper HTML, that is, all the tags inside of it are properly closed.