Coder Perfect

Using a PHP variable and a string literal together


Let’s say I have a variable named $test that is defined as follows: ‘cheese’ as $test

I’d like to produce something cheesey, which I can do in the following way:

echo $test . 'y'

However, I’d rather simplify the code to something like this (which won’t work):

echo "$testy"

Is there a way to handle the y as if it were a separate variable from the variable?

Asked by Matt McDonald

Solution #1

echo "{$test}y";

When interpolating variables directly in strings, braces can be used to eliminate ambiguity.

Also, single quotations aren’t supported. So:

echo '{$test}y';

will output


Answered by alex

Solution #2

You can use arround to separate your variable from what comes after it:

echo "{$test}y"

You can refer to the PHP manual’s Variable parsing – Complex (curly) syntax section for more information.

Answered by Pascal MARTIN

Solution #3


$test = "chees";

It will output:

It’s precisely what you’ve been looking for.

Answered by Rizwan

Solution #4

$bucket = '$node->' . $fieldname . "['und'][0]['value'] = " . '$form_state' . "['values']['" . $fieldname . "']";

print $bucket;


$node->mindd_2_study_status['und'][0]['value'] = $form_state['values']

Answered by earlyburg

Post is based on