Have you looked into either a framework or a templating language? Personally, I'm a big fan of Twig for templates if you're spinning your own PHP or can choose an output vector. As far as frameworks are concerned, I actually like the new CodeIgniter (please don't judge me - I think it's got a lot of the functionality of other modern frameworks without being so opinionated or bloated) but I'm currently working with Laravel, which is a thing a lot of people use.
Either way, separating your business logic and display is a good thing - you don't have to go full MVC, but it does make life easier once you get used to it. For instance, in your setup if you need to change the heading level of something you risk breaking the functionality of the data flow instead of just the display because the data gathering SQL is in the same function as the HTML output. If the output is in a separate file there's far less danger in making a change to that output - and you can use the gathered data anywhere else you might need it (exporting, pdf, etc).