We help IT Professionals succeed at work.

PHP $_POST in string

erzoolander asked
Medium Priority
Last Modified: 2012-05-12
Hey - quick and possibly boneheaded question.

If I'm building a string...say...

$body = "hello there variable one, variable two, variable three"...

and I want to substitute those variables from items in a $_POST array...

Is there a quick way for me to say...

$body = 'hello there $_POST['variable1'] $_POST['variable2']' etc etc...without having to break it up as...

$body = "Hello there";
$body .= $_POST['variable 1'];
$body .= etc etc?

When I attempt to do...

$body = 'hello there'.$_POST["variable1"].' '.$_POST["variable2"]'; etc...
it seems to glitch...keeping the actual PHP code in the string...and not the gathered array items.

Watch Question

ok you are going to have to remember the differences between single quote ' and double quote " in php.
Double quoted strings parse out variables, and single quoted ones do not. You can also freely mix them up so that if you have a single quoted string you can have all the double quotes you want in it and vice versa, just as long as you close off the string with the same one that you started with.
For this
$body = 'hello there'.$_POST["variable1"].' '.$_POST["variable2"]';
I would do it like this.
$body = "hello there $_POST['variable1'] $_POST['variable2']"; no string concatenation needed.


Yeah - found the problem right after I typed in the question - stupid typo...  :)

500 pts to you!