Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
161 views
in Technique[技术] by (71.8m points)

Why is the php string concatenation operator a dot (.)?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think it is a good idea to have a different operator, because dot and plus do completely different things.

What does "a string" + "another string"; actually mean, from a non specific language point of view?

Does it mean

  • Add the numerical value of the two strings, or,
  • concatenate the two strings

You would assume it is the second, but a plus sign is used for numerical addition in all cases except Strings. Why?

Also, from a loosely typed point of view (which PHP is), a php script

$myvar = 1;
$myvar2 = 2;

// would we expect a concatenation or addition?
$concat = $myvar + $myvar2;

The dot notation therefore specifies that it is clearly used for concatenation.

It is not that it is confusing, it is that it is not intuitive because all the other languages do it in a different way. And, is this a good reason to follow the trend? Doing things the way they are always done, is not always the right way.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...