Reputation: 5050
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?
Upvotes: 184
Views: 259513
Reputation: 239
"${test}y"
is deprecated since PHP 8.2, use "{$test}y"
instead. (Note the $
being covered by the curly braces.)
Upvotes: 5
Reputation: 89
Example:
$test = "chees";
"${test}y";
It will output:
cheesy
It is exactly what you are looking for.
Upvotes: 8
Reputation: 401022
You can use {}
arround your variable, to separate it from what's after:
echo "{$test}y"
As reference, you can take a look to the Variable parsing - Complex (curly) syntax section of the PHP manual.
Upvotes: 62
Reputation: 11
$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']
Upvotes: -2
Reputation: 490273
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
Upvotes: 318