Run your output past a validator... best way to find them.
Though if your opening and closing tags are in different php files, there's probably something horribly wrong with your planning -- though that's why I like to have a theme.php file that has functions for opening and closing sections, and output of what goes in-between... that way the actual output code isn't merged into my data retrieval/generation.
Basically, if you're having those types of issues, how you're building your php could be the real problem. Do you have an example of the code where you are having those types of problems? If we see what you are doing, I'm certain any number of people here could coach you in better ways of building code to avoid those types of problems.