adding to a string – PHP

  php

Q(Question):

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

A(Answer):

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.

A(Answer):

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.

A(Answer):

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/

A(Answer):

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 =–

A(Answer):

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.

A(Answer):

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>

LEAVE A COMMENT