469,270 Members | 1,092 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,270 developers. It's quick & easy.

adding to a string

hi all,

I have a string $string

I initialize the string:
$string = "test string<br>";

I want to add text to the string along my script but:
$string += "more information<br>";

doesn't seem to work.

Someone an help me?

regards
Stijn
Jul 17 '05 #1
6 48940
El Dijous 29 Gener 2004 16:49, Stijn Goris va escriure:

Hi!
I want to add text to the string along my script but:
$string += "more information<br>";

doesn't seem to work.


Try:
$string .= "more information<br>";

Regards

--

En cap cap cap el que cap en aquet cap.
Jul 17 '05 #2
zapf wrote:
$string += "more information<br>";

doesn't seem to work.

Try:
$string .= "more information<br>";


Yeah, but why didn't they overload the + operator like other languages?
Because for concatenation, we have ., which is traditionally used for
object access. For that, we have the C++ object pointer operator, ->.
Not that I'm complaining, but it seems to be an unnecessary deviation
from the norm in such a fundamental area.

Jul 17 '05 #3
With total disregard for any kind of safety measures Keith Bowes
<do****@spam.me> leapt forth and uttered:
zapf wrote:
$string += "more information<br>";

doesn't seem to work.

Try:
$string .= "more information<br>";


Yeah, but why didn't they overload the + operator like other
languages?
Because for concatenation, we have ., which is traditionally
used for
object access. For that, we have the C++ object pointer
operator, ->. Not that I'm complaining, but it seems to be an
unnecessary deviation from the norm in such a fundamental area.


Becuase thats the way PHP works. The . operator for concatenation
was borrowed from Perl, as was the -> operator (although it
originated in C++)

In PHP += Is used in numeric calculations, I think $i += 2 is the
same as $i = $i + 2.

--
Phil Roberts | Nobody In Particular | http://www.flatnet.net/
Jul 17 '05 #4
Keith Bowes wrote:
Yeah, but why didn't they overload the + operator like other languages?
Because for concatenation, we have ., which is traditionally used for
object access. For that, we have the C++ object pointer operator, ->.
Not that I'm complaining, but it seems to be an unnecessary deviation
from the norm in such a fundamental area.


so that you can do:

<?php
$n = 3;
$s = '6';

echo $n + $s; // 9
echo $n . $s; // 36
echo $s + $n; // 9
echo $s . $n; // 63
?>
compare with my feeble attempt at C++

#include <iostream.h>

int main(void) {
int n=3;
char* s="6";
// I don't know how to display 9 or 36
// as the result of an operation over n
// and s now
cout << n << s << "\n"; // ok for 36 :-)
return 0;
}
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #5
it also helps if you are looking at some code that was written a while back,
and you are midstream and see:

$var .= $var2;

you know this is string concatenation not a math addition.

if you were to see:

$var += $var2

you would have no idea if it is $var = $var + $var2 or if it is $var =
"$var$var2"
this is another reason it is serperate.

just my $.02

--
Mike Bradley
http://www.gzentools.com -- free online php tools
"Keith Bowes" <do****@spam.me> wrote in message
news:nx*******************@fe02.usenetserver.com.. .
zapf wrote:
$string += "more information<br>";

doesn't seem to work.

Try:
$string .= "more information<br>";


Yeah, but why didn't they overload the + operator like other languages?
Because for concatenation, we have ., which is traditionally used for
object access. For that, we have the C++ object pointer operator, ->.
Not that I'm complaining, but it seems to be an unnecessary deviation
from the norm in such a fundamental area.

Jul 17 '05 #6
On Thu, 29 Jan 2004 12:35:45 -0500, Keith Bowes <do****@spam.me> wrote:
$string += "more information<br>";

doesn't seem to work.


Try:
$string .= "more information<br>";


Yeah, but why didn't they overload the + operator like other languages?


It's a matter of opinion as to whether this:

<pre>
<?php
print "1" + "2";
print "\n";
print "1" . "2";
?>
</pre>

Should output:

3
12

(which it does)

Or:

12
12

Both are reasonable from different points of view; PHP went with the first.
Given that numeric values often come in as strings from $_GET/$_POST, always
doing arithmetic for '+', and having concatenation as a separate operator
probably makes more sense in the majority of cases.

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
<http://www.andyh.co.uk> / <http://www.andyhsoftware.co.uk/space>
Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

23 posts views Thread by YinTat | last post: by
2 posts views Thread by John Tyce | last post: by
3 posts views Thread by Jim Heavey | last post: by
14 posts views Thread by Paul_Madden via DotNetMonster.com | last post: by
8 posts views Thread by gthorpe | last post: by
34 posts views Thread by Larry Hastings | last post: by
1 post views Thread by mwcapps | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.