Problem
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
{$test}y
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
Example:
$test = "chees";
"${test}y";
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;
yields:
$node->mindd_2_study_status['und'][0]['value'] = $form_state['values']
['mindd_2_study_status']
Answered by earlyburg
Post is based on https://stackoverflow.com/questions/5368890/mixing-a-php-variable-with-a-string-literal