Figured out the problem: don't convert an HTML template to PHP by simply adding PHP extension
For the longest time I had issues with DW deleting php code even when codeOutside text was set properly, but it would do it on some sites, and not on others - seemingly without reason. Just in case anyone else runs into this, I figured out what the problem is. It seems to be that you can't just add '.php' to the end of a '.dwt' template file to change an HTML template to a PHP template. For reasons I have yet to fully understand, doing so causes DW to do goofy things. You need to make sure if you intend to do a PHP site, you're starting with a PHP file, and then doing save as a PHP template.