Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 109,716 PHP Programmers for FREE! Ask your question and get quick answers from experts. There are 1,995 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Wouldn't it be great if adding two strings just concatenated them

 
Reply to this topicStart new topic

Wouldn't it be great if adding two strings just concatenated them

geethalakshmi
post 16 Jul, 2008 - 06:10 AM
Post #1


New D.I.C Head

*
Joined: 9 Apr, 2008
Posts: 13

When I add two strings together and then echo it, it echoes zero instead of the concatenation of the two strings! What's going on? Wouldn't it be great if adding two strings just concatenated them together?

Regards,
geetha
Tools And Resources
User is offlineProfile CardPM

Go to the top of the page


AdaHacker
post 16 Jul, 2008 - 06:53 AM
Post #2


D.I.C Head

**
Joined: 17 Jun, 2008
Posts: 77



Thanked 10 times
My Contributions


The string concatenation operator in PHP is '.', not '+'. Using the '+' operator will convert the two strings to numeric types and then add the resulting values. If the strings are not numeric, then they'll cast to 0, as you've seen. And, as an interesting side-note, if a string starts with a number but contains non-numeric characters, PHP will truncate everything from the first non-numeric and convert just the leading digits to an int.

And no, it wouldn't be great if it just concatenated them, because then you'd have to worry about the contents of the string. Should the result of "123" + "456" be 579 or "123456"? What about "123" + "5.2x"? Is it really a good thing to have to worry about that? PHP has enough gotchas already without adding ambiguity to one of the standard operators.
User is online!Profile CardPM

Go to the top of the page

mocker
post 16 Jul, 2008 - 10:23 AM
Post #3


D.I.C Head

**
Joined: 14 Oct, 2007
Posts: 177



Thanked 6 times
My Contributions


I much prefer having php do "5" + "3" = 8, rather than "5"+"3" = 53 by default. Remembering to type . instead of + isn't exactly stressing me out, and php isn't the only language to use it.
User is offlineProfile CardPM

Go to the top of the page

mattisdada
post 16 Jul, 2008 - 11:29 PM
Post #4


New D.I.C Head

*
Joined: 30 May, 2008
Posts: 35


My Contributions


QUOTE(geethalakshmi @ 16 Jul, 2008 - 06:10 AM) *

When I add two strings together and then echo it, it echoes zero instead of the concatenation of the two strings! What's going on? Wouldn't it be great if adding two strings just concatenated them together?

Regards,
geetha
Tools And Resources

Are you sure your putting a " . " between each string? Eg,
CODE
$hi = "hi " . $name . ", how are you?";

User is offlineProfile CardPM

Go to the top of the page

no2pencil
post 16 Jul, 2008 - 11:41 PM
Post #5


Wet D.I.C.

Group Icon
Joined: 10 May, 2007
Posts: 4,950



Thanked 27 times

Dream Kudos: 2325

Expert In: Goofing Off

My Contributions


QUOTE(geethalakshmi @ 16 Jul, 2008 - 09:10 AM) *

What's going on?

...it is impossible to answer that question without seeing your source code.

Maybe you can help with my car, it's making this clank clank noise. Any ideas?
User is offlineProfile CardPM

Go to the top of the page

mattisdada
post 17 Jul, 2008 - 12:04 AM
Post #6


New D.I.C Head

*
Joined: 30 May, 2008
Posts: 35


My Contributions


QUOTE(no2pencil @ 16 Jul, 2008 - 11:41 PM) *

QUOTE(geethalakshmi @ 16 Jul, 2008 - 09:10 AM) *

What's going on?

...it is impossible to answer that question without seeing your source code.

Maybe you can help with my car, it's making this clank clank noise. Any ideas?

Obviously you have a faulty spark plug. Try reformating your exhust.
User is offlineProfile CardPM

Go to the top of the page

no2pencil
post 17 Jul, 2008 - 12:16 AM
Post #7


Wet D.I.C.

Group Icon
Joined: 10 May, 2007
Posts: 4,950



Thanked 27 times

Dream Kudos: 2325

Expert In: Goofing Off

My Contributions


QUOTE(mattisdada @ 17 Jul, 2008 - 03:04 AM) *

Obviously you have a faulty spark plug. Try reformating your exhust.

The mechanic told me I had a unicorn in my muffler...
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 9/8/08 09:15AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month