logo
Search:

Login:


Forgot Details? Sign-up

forum >> Programming questions >> HTML / XHTML / HTML5 / CSS

Concatenation

Posted May 11 2012 at 6:08 PM by
Pete Woodhead (petewdhd)
This question concerns Concatenation. In mini book V chapter 6 you provide an example where you place contact information into a CSV format. I wondering why the dots (.) appear after $output just ahead of the equal symbol,=, beginning on the second line even though they also follow the variable name on each line? Through some trail and error I have found that the data doesn't seem to string properly unless it's done this way, I'm just in the dark as to why. Is it because the tabs are in between? I've cut a pasted a sample from chapter 6 below.
Thanks,
Pete
Code:
//generate output for text file $output = $fName . "\t"; $output .= $lName . "\t"; $output .= $email . "\t"; $output .= $phone . "\n";
AuthorMessage
Pete Woodhead
Posted: May 14 2012 kl. 1:45 PM

Okay, looking at this fresh today (a couple of days later), I believe I see what I was overlooking before.
$output = $fName . "\t";
$output .= $lName . "\t";
$output .= $email . "\t";
$output .= $phone . "\n";
I believe this is saying that $output variable is the value of $fName variable and is concatenated to the tab symbol, which is concatenated to the $output variable as the value of the $lName variable which is concatenated to the tab symbol and so on until the $output variable value is the $phone variable which is concatenated to "new line" (\n) signaling a line break and an end of that string.

What I'm still uncertain of is why the dot comes ahead of the equal sign.
Andy
Posted: May 15 2012 kl. 10:51 PM

Love your questions. Keep 'em coming!!!

The dots are the concatenation operator. (Like that's helpful...)
You can combine strings together to make a new string, like this:
Code:
$a = "whoo "; $b = "hoo"; print $a . $b;

This is called "string concatenation." In most languages, you can use the + operator to combine strings, but it's not addition, it's concatenation (which is I guess as close as you can get to adding strings.)

Remember that in PHP, the variable types are determined by the operator, so if you add $a and $b with the plus sign, it would try to interpret them as numeric values.

So, this doesn't explain the .= thing. Well, kind of, it does. See, the . is an operator just like the + sign.

Think of this operation:
Code:
$a = 5; $a += 3;
At the end of the operation, $a would contain 8, because $a += 3 is a shorthand for $a = $a + 3.

The .= operator is similar.
Code:
$a .= ", dude"
is the same as saying
Code:
$a = $a . ", dude"
Hope this helps!
-Andy
Pete Woodhead
Posted: May 16 2012 kl. 9:53 AM

Yes that helps. I had to go over it a few times because I'm just that slow, but finally my fog began to lift. Hahaha Thanks for going back over operators. Seeing the dot as an operator made it clearer for me. I still have trouble keeping all of this straight, so anytime I'm able to see it from a different angle it really helps it gel better.

Oh, and don't worry the questions will keep coming!