Mixing a PHP variable with a string literal Ask Question

Mixing a PHP variable with a string literal Ask Question

Say I have a variable $test and it's defined as: $test = 'cheese'

I want to output cheesey, which I can do like this:

echo $test . 'y'

But I would prefer to simplify the code to something more like this (which wouldn't work):

echo "$testy"

Is there a way to have the y be treated as though it were separate from the variable?

ベストアンサー1

echo "{$test}y";

You can use braces to remove ambiguity when interpolating variables directly in strings.

Also, this doesn't work with single quotes. So:

echo '{$test}y';

will output

{$test}y

おすすめ記事