RE: [PHP] String concatenation
| From: | Boget, Chris | Date: | Fri, 30 Jun 2000 14:15:31 +0000 |
| Subject: | RE: [PHP] String concatenation | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-4028@lists.php.net to get a copy of this message | ||
> 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:
Of course, you could always do:
echo "Hello, $myArray[this element] is an element of my array";
and eliminate the quotes within the []'s all together. I do this *all*
the time.
> 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";
I use interpolation almost everywhere. However, there are a few
times when I need to do the above and it is almost always when
I'm concatinating the result of a function. An example would be:
echo "We found " . @mysql_num_rows( $result ) . " record(s).<br>\n";
or
echo "Todays date is: " . date( "F d, Y" ) . "<br>\n";
HTH
Chris