Reputation: 1992
I have this code:
<?php
$attribute = 'name';
$value = 'John';
print_r("$attribute$value");
it outputs:
nameJohn
I want to keep using "" (double quotes) to output:
name_value: John
I tried:
print_r("$attribute_value: $value");
But of course it doesn't work (there is no variable $attribute_value).
How can I do it?
Upvotes: 0
Views: 284
Reputation: 2037
Assign 'name_value' to $attribute.
<?php
$attribute = 'name_value';
$value = 'John';
print_r("$attribute: $value");
Upvotes: 0
Reputation: 219794
Use curly braces to indicate what is a variable and what is a string:
print_r("{$attribute}_value: $value");
You can also use printf()
to format your string (or sprintf()` if you wish to capture it to a variable):
printf("%s_value: %s", $attribute, $value);
Upvotes: 6