Re: String concatenation
| From: | Laurie Voss | Date: | Fri, 30 Jun 2000 14:12:28 +0000 |
| Subject: | Re: String concatenation | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-4024@lists.php.net to get a copy of this message | ||
On 30 June 2000, php@theOrb.net wrote:
ptn> I've noticed that all of the documentation uses the dot (".")
ptn> operator to concatenate strings; however, I've also noticed that
ptn> the PHP parser will expand variables within a string. Is there
ptn> any compelling reason to use one method over the other? (For
ptn> clarity and the fewer typos I'd make I prefer the latter)
I bet a billion people will answer this one, but I may as well.
The reason there are two methods is because up to PHP3, the in-line
string concatenation was unreliable, e.g.:
echo "Hello, this is $mystring";
would work fine, but
echo "Hello, $myArray["this element"] is an element of my array";
would not, because the first " mark after the [ would terminate the
echo, and cause a parse error. In PHP 4 you avoid that problem using
the syntax:
echo "Hello, {$myArray["this element"]} is an element of my array";
This works, but many people (including me), thinks this looks MORE
confusing than the alternative:
echo "Hello, " . $myArray["this element"] . " is an element of my
array";
But it's a matter of choice. Also, you can use the dot-operator to do
things like call functions, do math, etc., which you can do by simply
calling a variable.
Seldo.
--
Laurie Voss -- www.seldo.com
laurievoss@email.com
If life isn't worth living, what else can you do with it?