Yes, that would work - but why go to all that effort? Install mod_gzip (still assuming you're using Apache) and you won't have to go changing all your files, and you won't have to run PHP on every single one of them.
It's a matter of using the right tool for the job. Using PHP for this is like using a powersaw to hammer in a nail. It can be done, but it's a bit silly and people watching are likely to snigger and point.