When you open a file for writing with >, it is truncated before you even read it. That's why it's empty.
Alternative with a temporary file (yes, this is quite common):
php -r "…" < build/index.php > tmp
mv tmp build/index.php
Ideally, use mktemp to generate a unique temporary file:
tmp="$(mktemp /tmp/file.XXX)"
php -r "…" < build/index.php > "$tmp"
mv "$tmp" build/index.php
But this isn't really shorter or more concise.
My personal favorite is the sponge utility from moreutils – it reads input and then writes to the file:
php -r "…" < build/index.php | sponge build/index.php
Or, better yet, don't do the substitution in PHP, but a tool that supports in-place editing, like Perl with:
perl -pi -e '…' build/index.php
Similar with sed, here the in-place flag for GNU sed:
sed -i -e '…' build/index.php
And for BSD sed, you need an explicit empty argument for -i:
sed -i '' -e '…' build/index.php
You will probably have to adapt your regular expression / substitution command for Perl's and sed's syntax, respectively.