If you wrap in double quotes, you will have to escape out the $ symbol as well. Better to wrap the whole thing in single quotes, and then single quotes are the only characters you'll have to escape out within the PHP snippet.
So the example given above should actually be:
Code:
echo '<?php $a="foo"; $b="bar"; echo $a.$b; ?>' | php