...

View Full Version : Resolved Newbie concatenation vs append question



PonchoX
02-01-2010, 06:35 AM
hi.

i'm learning PHP. Early stages.

Can somebody tell me why the simple concatenation period "." doesn't work in this example?

<?php
$firstString = "The quick box fox";
$secondString = " jumped over the lazy dog.";
$thirdString = $firstString;
$thirdString .= $secondString;
echo $thirdString;
?>

The above code works and outputs "The quick box fox jumped over the lazy dog."

BUT! If I remove the equal-sign so that the code in the next-to-last line looks like this:

$thirdString . $secondString;

I only get outputted > "The quick box fox"

Why no "jumped over the lazy dog."?

What happened to the value for the $secondString?

Doesn't concatenate put the two together?

Or does it work only for strings and not variables?

bdl
02-01-2010, 06:53 AM
If you remove the assignment operator, there's no assignment. The variable $thirdString doesn't add the $secondString variable value. It may be a valid expression in that it doesn't cause the interpreter to fail, but it doesn't "do" anything. Now, if you were to do this:



$firstString = "The quick box fox";
$secondString = " jumped over the lazy dog.";
$thirdString = $firstString;

echo $thirdString . $secondString;


That concatenation operator has some effect.

abduraooft
02-01-2010, 06:57 AM
BUT! If I remove the equal-sign so that the code in the next-to-last line looks like this:

$thirdString . $secondString;

I only get outputted > "The quick box fox"

Why no "jumped over the lazy dog."?

What happened to the value for the $secondString?
. is just doing the concatenation bu the statement
$thirdString .= $secondString; is equivalent to

$thirdString = $thirdString.$secondString;.

Thus when you remove the =, it reads like

$thirdString . $secondString; and thus it's not assigning anything to the variable $thirdString.

However the following statement will show you the same output.

echo $thirdString . $secondString; (though it won't modify the value of $thirdString)

PonchoX
02-01-2010, 07:48 AM
okay, i see.

the concatenation is not permanent, but only valid for that one statement, which i didn't echo.

the last statement, which i DID echo did not pick up the $secondString value cuz it hadn't become permanently attached to $firstString by the concatenation operator.

yes?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum