I currently have a very simple page, with some more complicated backend. I\'ve always done this the same way, but I feel it\'s not right. But I haven\'t come up with much us
I think most experienced coders would tell you to mix your code and your HTML as little as possible. This means doing all of your calculations, then simply passing any variables needed by the view to the view.
They would also tell you to look into the MVC design pattern. In PHP, this is where Controller classes take the initial request, perform the correct operations on the correct business Model classes & then pass needed data to a View class, which would render your HTML.
MVC can go from very simple 3 class variations, to extremely complex "Frameworks" like Zend Framework. If you build your own, look into PHP class autoloading, as it will make your life easier.