I want to add variables into a string in php. I googled and found the method of : "{$a}". However this doesn't work for me. I echoed it and it has literally echoed "{$a}" instead of the actual value of the variable. Here is my code:
$body= json_decode(
'{
"sender_batch_header":
{
"email_subject": "SDK payouts test txn"
},
"items": [
{
"recipient_type": "EMAIL",
"receiver": "{$email}",
"note": "Your 1$ payout",
"sender_item_id": "Test_txn_12",
"amount":
{
"currency": "CHF",
"value": "{$actualAmount}"
}
}]
}',
true);
And here is the echo of $body:
{ "sender_batch_header": { "email_subject": "SDK payouts test txn" }, "items": [ { "recipient_type": "EMAIL", "receiver": "{$email}", "note": "Your 1$ payout", "sender_item_id": "Test_txn_12", "amount": { "currency": "CHF", "value": "{$actualAmount}" } }] }
The variables I am talking about are $email and $actualAmount. I debugged it and they both have a value it just does not get added to the string. Can anyone help?
A string literal can be different ways:
You can use braces in double-quoted strings like: "Hello {$foo}" unfortunately your string variable is single-quoted. So we can use the concatenation operator ('.'), which returns the concatenation of its right and left arguments.
So let's try with concatenation operator;
$body= json_decode(
'{
"sender_batch_header":
{
"email_subject": "SDK payouts test txn"
},
"items": [
{
"recipient_type": "EMAIL",
"receiver": "'.$email.'",
"note": "Your 1$ payout",
"sender_item_id": "Test_txn_12",
"amount":
{
"currency": "CHF",
"value": "'.$actualAmount.'"
}
}]
}',
true);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With