ShangNing.net

 
A curious phenomenon in PHP language

This entry was posted on Monday, July 5th, 2010.
Written by: ShangNing

Today’s afternoon, I wrote an aimless code of PHP, that’s a good way to waste time I think. Then I created a simple line, and I assume the codes was wrong. But when I run the code, its output  amazes me. The simple codes is:

<?php
print 1…1;
?>

The output is “10.1″, I have in mind some doubts which really puzzle me. Why is that? And I wrote more codes to try it:

<?php
print 1.1;    // The output is 1.1, one dot, the result is really clearly
print 1..1;   // Wrong, two dot, error clearly
print 1…1;  // 10.1
print 1….1; // Wrong, four dot, yeah, error of course
print 2…2;  // 20.2
print 6…3;  // 60.3
?>

Only three dots mixed me up more, there is no the “three dots” operator in PHP language. So I wrote more testing codes:

<?php
$first=7;
$last=6;
print $first$last;        // Output is 70.6
print ($first.).(.$last);    // Output is 70.6 too, em, I think its nearly the answer…
print ($first.);               // wrong
print 7.;                       // wrong
// oh, no, I think I don’t know why…
?>

Last now, I still don’t know why this is. So, I submit it on PHP community and hope to get the question’s answer. If you know, please tell me, thank you.

 

9 Responsesto “A curious phenomenon in PHP language”

Khwab
July 6th, 2010 at 12:32 am

The reason is 1. is equal to 1 and .1 is equal to 0.1 so 1…1 is equal to 1 . 01

so the above example becomes

which outputs 10.1;

Rahul
July 6th, 2010 at 1:15 am

@Khwab you mean to say that here 2nd dot is treated as concatination operator.

Mathayi
July 6th, 2010 at 1:23 am

The middle dot will act as concatenation operator

1…1 => ‘1′ . ‘0.1′ => ‘10.1′

NB: 1. => 1 and .1 => 0.1

Khwab
July 6th, 2010 at 3:44 am

Yeah..

tvl
July 8th, 2010 at 12:16 pm

sure, but that does’t explain why print 1..1; doesn’t work

mario
July 8th, 2010 at 4:57 pm

Amazing. I can’t wait to spread this throughout some codebases.. ;}

Michael Restuccia
July 9th, 2010 at 11:10 pm

@tvl

It could be because its expecting a value between the concatenation operator.

Example

‘1′ . $value . ‘1′;

instead its ‘1′ .. ‘1′.

ShangNing
July 11th, 2010 at 9:34 am

wow, I think I have got the answer, thanks a lot for all the friends :)

Mike
July 24th, 2010 at 9:40 pm

check out http://www.stackoverflow.com/ … its a great community to answer coding/developing questions like these.

Leave a Reply






Recent Entries