Sergej Fomin
Sergej Fomin

Reputation: 1992

PHP double quotes

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

Answers (2)

David J Eddy
David J Eddy

Reputation: 2037

Assign 'name_value' to $attribute.

<?php

$attribute = 'name_value';
$value = 'John';

print_r("$attribute: $value");

exp: https://3v4l.org/QBepr

Upvotes: 0

John Conde
John Conde

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

Related Questions