Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,109 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:

<?php

$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.

Cheers!