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 Integrating with PayPal Array Keys

This has me stumped !

I clearly do not understand concatenation as both tasks it involves have had me requiring help??

Would some body please enlighten me??/ get me past this task??

Craig

2 Answers

Erik McClintock
Erik McClintock
45,783 Points

Craig,

I see; no worries! This should be simple enough :)

Concatenation is simply the act of appending one thing to another (see Merriam-Webster for more).

In PHP, the character used isn't as intuitive as in other languages (frequently, the plus sign [ + ] is used to represent this), but the usage is the same. To help understand it, try to think of it simply as the addition operator in basic mathematics:

x + y = result

In PHP, you would write that same statement as:

x . y = result

The ' . ' simply replaces the ' + ' in PHP.

So, if my full name is Erik McClintock, and I had my first and last names stored in their own variables, I could echo my full name by appending the lastName variable to the firstName variable (with a space added in the middle for readability), as follows:

$firstName = "Erik";
$lastName = "McClintock";

$fullName = $firstName . " " . $lastName;

echo $fullName;

So, here, we declare both the firstName and lastName variables, then declare a new variable $fullName to hold my full name, and assign it to be the result of linking (or "concatenating") my firstName, a space in between, and then my lastName. Thus, when we echo $fullName, the value we'll get will be "Erik McClintock".

You could add my fullName into a sentence with some simple concatenation, as well:

echo "Hello, my name is " . $fullName . ".";

This would echo "Hello, my name is Erik McClintock."

The . "."; at the end of the statement is not adding another concatenation, it is adding a period at the end of the sentence. This is really where the PHP character of the dot can be off-putting.

That same thing written without the space (which can make it look a little confusing when you're first learning it) would look like this:

$firstName = "Erik";
$lastName = "McClintock";

$fullName = $firstName . $lastName;

echo $fullName;

And that would produce "ErikMcClintock". There is only one ' . ' used to concatenate one thing to the next in your statement. If you had a word broken up by syllables and you wanted to echo it out put back together, you would do the following:

$firstSyllable = 'con';
$secondSyllable = 'cat';
$thirdSyllable = 'en';
$fourthSyllable = 'ate';

$wholeWord = $firstSyllable . $secondSyllable . $thirdSyllable . $fourthSyllable;

echo $wholeWord;

$wholeWord then would echo 'concatenate'. Notice there is only one dot between each variable to append the previous to the next.

In the code you posted, you're adding too many ' . ' to concatenate. If you think of the situation as needing plus signs ' + ' to work, it may be easier to visualize, and you can then simply replace the plus signs with the PHP dot character.

Does this make any more sense?

Erik

A genuine hero!!

And a really good example / explanation I put + signs where I needed my space with all the other syntax as normal and worked from there was great help!

Very much appreciated!

Erik McClintock
Erik McClintock
45,783 Points

Great! Glad I could help!

Happy coding :)

Erik

Erik McClintock
Erik McClintock
45,783 Points

Craig,

What are you struggling with specifically? The concept of concatenation, the syntax, the wording in the challenge?

If you post your issue (along with whatever code you're trying that isn't working/doesn't make sense), we can be of more assistance!

Erik

Sorry Erik I was a little vague due to frustration,

On the code challenge the first part seemed simple enough

drop in --- ($books as $isbn => $book) -- All seems Well

Then comes what I believe is my need to understand concatenation, seriously the whole concept of the " . " . " when coming after an arrays variable just isnt sinking in.

The code I had input intothe second part of the challenge was:

<li><?php echo $book . "(" . $isbn . ")" ?></li>

My apologies if it is painful to look at such a disaster of code !!

Thanks for your Help!

Craig