Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Build a Simple PHP Application Adding a Contact Form Concatentation

I am very Stuck :(

I do not understand what concatenation does and I cannot get past this task :(

2 Answers

Kevin Korte
Kevin Korte
28,148 Points

Concatenation joins two things together.

For instance I could concatenate the results of a function or the value of a variable with a string

//Let's get the day of the week
$dayOfWeek = date( 'l' );
//This will get a value of "Monday, Tuesday, Wednesday...etc depending on the day of the week it actually is

echo "Howdy stranger, today is " . $dayOfWeek . ".";

Today, this would output: Howday stranger, today is Wednesday.

The period mark indicates a concatenation.

Peter, Kevin,

Going a little bit beyond the concatenation discussion; while concatenation is really useful, I want to point out the difference between single quotes and double quotes when dealing with variables. Take a look at the following code:


$var = 'foo';
echo 'The value of variable var is $var.';
echo "The value of variable var is $var.";


The first echo statement will be: The value of variable var is $var. While the second echo statement will be: The value of variable var is foo.

In PHP, single quoted strings will display the name of a variable while a double quote will display the value of the variable. Just a fun fact that is useful when you want to include a variable in a string you are echoing. You can do either way and it all depends on how you are programming and the style, but either concatenation or embedding a variable using double quotes will give you the same thing. Concatenation is good because it allows you to find the variables that you are adding to a string easily. I use the double quoted way as I trained myself to find variables by the $ and I can find variables with ease inside any PHP script.

I know this is a little bit out of scope of not only this post, but the subject of discussion, however I thought you might find the alternative a little bit interesting.