Oh, so you're wanting to run PHP from within PHP, rather than running it yourself from the command line.
What you've got looks about right. here is something I put together when I was generating and syntax-checking code on the fly (I've got the code coming from a file there, but the real code was a bit more elaborate, so the posted code is still a bit more elaborate than strictly necessary for what it does).