By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,825 Members | 1,697 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,825 IT Pros & Developers. It's quick & easy.

String concatenation: why period?

P: n/a
Why did the designers of PHP decide to use period (".") as the string
concatenation operator rater than the obviously more logical plus
("+")?

//JJ
Jul 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On 20 Aug 2004 15:40:32 -0700, jo******@yahoo.com (Jonathan) wrote:
Why did the designers of PHP decide to use period (".") as the string
concatenation operator rater than the obviously more logical plus
("+")?


Cos that's how Perl does it, and PHP was originally written in Perl?

It separates arithmetic operations from string operations; is it more logical
that "1"+"2" == "12", or 3? By picking a separate concatenation operator, you
remove the ambiguity and let you have both ways available.

"1"+"2" == int(3)
"1"."2" == string(2) "12"

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

P: n/a
>Why did the designers of PHP decide to use period (".") as the string
concatenation operator rater than the obviously more logical plus
("+")?


More logical to me having the period.. but I come from a perl background
before I learned PHP..

Regards,
Chris
Jul 17 '05 #3

P: n/a
"Andy Hassall" <an**@andyh.co.uk> wrote in message
news:o1********************************@4ax.com...
Cos that's how Perl does it, and PHP was originally written in Perl?


AFAIK, PHP has also been in C. Who here has used the original PHP/FI?

Jul 17 '05 #4

P: n/a
Skeleton Man wrote:
Why did the designers of PHP decide to use period (".") as the string
concatenation operator rater than the obviously more logical plus
("+")?

More logical to me having the period.. but I come from a perl background
before I learned PHP..

Regards,
Chris


Originally PHP == Perl Hypertext Preprocessor. Later it was changed to PHP:
Hypertext Preprocessor. It was originally just a collection of Perl Scripts to
do "stuff" and was eventually formalized into it's own scripting language that
resembles it's Perl and C roots.

--
Michael Austin.
Consultant - Not Available.
Donations still welcomed. Http://www.firstdbasource.com/donations.html
:)
Jul 17 '05 #5

P: n/a
Michael Austin wrote:
Skeleton Man wrote:
Why did the designers of PHP decide to use period (".") as the string
concatenation operator rater than the obviously more logical plus
("+")?
More logical to me having the period.. but I come from a perl background
before I learned PHP..

Regards,
Chris


and if I had read all of the history, see: http://www.php.net/manual/en/history.php
it's ORIGINAL name was:
Personal Home Page Tools/Forms Interpreter. I had only heard the response I gave
earlier.
Originally PHP == Perl Hypertext Preprocessor. Later it was changed to
PHP: Hypertext Preprocessor. It was originally just a collection of
Perl Scripts to do "stuff" and was eventually formalized into it's own
scripting language that resembles it's Perl and C roots.


--
Michael Austin.
Consultant - Available.
Donations welcomed. Http://www.firstdbasource.com/donations.html
:)
Jul 17 '05 #6

P: n/a
jo******@yahoo.com (Jonathan) emerged reluctantly from the curtain
and staggered drunkenly up to the mic. In a cracked and slurred
voice he muttered:
Why did the designers of PHP decide to use period (".") as the
string concatenation operator rater than the obviously more
logical plus ("+")?

//JJ


One of the major problems is that PHP, like Perl, allows arbitrary
typecasting between strings and numbers. This means that unless you
have specific operators for string concatenation and numeric
addition you're going to run into problems with variable typing.

For example:

echo "2" + "2";
echo 2 + 2;
echo 2 + "2";

All will produce the same result, but:

echo 2 . "2"; -> "22"
echo 2 . 2; -> "22"

Even though the numbers in the first and second example are
strings. PHP simply typecasts the strings to intergers before
performing the math. In the 4th and 5th examples PHP typecasts the
intergers to strings before concatenating them. In any language
with strict typing this would result in an error.

--
Phil Roberts | Deedle Doot Doo Dee Dee | http://www.flatnet.net/

I could be wrong here, You could be right
Please forgive me I have sinned - Not on your life
But that's how you want me, But I'll never fear thee
Why you and not me? Tell me Holy Man
Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.