开发者

Dot operator in PHP

开发者 https://www.devze.com 2023-03-20 23:27 出处:网络
I thought I\'ve known the String Operator . well enough until I was asked a question about it today. The question looks quite simple:

I thought I've known the String Operator . well enough until I was asked a question about it today. The question looks quite simple:

echo 100...100;

At the first glance I thought it would ma开发者_如何学JAVAke a syntax error. But when I ran the code and saw the result I was totally confused. The result is

1000.1

So I wonder how could this happen?

Thanks.


Read it like this:

(100.) . (.100)

Thus it concats 100 and 0.1.


Assuming you meant

echo 100...100;

The reason for this is the beauty of PHP. :) This statement is understood as

100. . .100

which is equivalent to

100.0 . 0.1

<=>

'100' . '0.1'

<=>

'1000.1'


You can read it as echo 100 . 0.1.


Actually, that only works without the quotes:

echo "100...100";   100...100  << with quotes the . is just a char

echo 100 . 100;     100100     << two concatenated strings "100"

echo 100.100;       100.1      << 100.100 is just a number

echo 100...100;     1000.1     << what you asked

echo 100. . .100;   1000.1     << what PHP actually interprets
0

精彩评论

暂无评论...
验证码 换一张
取 消