I have the following in myfile.php
$json_data = '{"type":"email","msg":"some text"}';
Instead of typing “email” or “some text” I want to concat the php variables $thetype and $themsg to the line before.
How can I do that? No matter what I do I get a syntax error.
I’m trying:
$json_data = '{"type":"+$thetype+","msg":"+$themsg+"}';
But as I say errors galore.
Thanks a lot
Your question is a little vague…
Are you looking for this?
That will set
$json_datato a string like what you described:Would print:
See the PHP manual for json_encode.
You could try and build the string by hand, like this:
But, you’ll generally be better off using json_encode, as it’s designed for this purpose and is much less likely to produce invalid JSON.