How do I concatenate variables in a PHP doc?

Daniel

New member
For example, how can I concatenate a first name variable and a last name variable into a full name variable? Preferably using the POST method. Thanks for any help!
 
I'll assume you mean when the POST form data has arrived at the server after the user has filled in and submitted the form.

Let's assume the form input names are "firstname" and "lastname". To cat them together, you just use the dot (.) operator, like this:

echo "The user's full name is " . $_POST["firstname"] . $_POST["firstname"] . ".";

The last dot inside the quotes is just a period, so the line on the page will look like this (assuming the user has entered John and Smith):

The user's full name is John Smith.

You can also assign the POST values to variables on the server side and use them in the echo statement. What I did here was a way to use the POST values directly.
 
PHP does allow you to concatenate strings with the "." operator

$name = $firstname . $lastname;

The above line concatenates firstname and lastname into name.

<?php echo $_POST["name"]; ?>

we use the $_POST function in PHP to collect data from the html form.
 
Back
Top