1

Just a second ago I was playing around with PHP, trying to figure out if there was a native range function (eventually finding range). However, one of the things I tried was the following:

echo 1...2;

which to my surprise returns the string "10.2". Can anyone tell me exactly what syntax is responsible for this? It doesn't seem like a valid place for a splat operator.

janw
  • 8,758
  • 11
  • 40
  • 62
  • 2
    you tried `1...2`, what does that mean? There is a lot missing from this question, if you are referring to some code, you need to include it, not just vaguely allude to it –  Aug 26 '19 at 21:35
  • @tim you can reproduce it with just `echo 1...2;` – Barmar Aug 26 '19 at 21:39
  • yeah, well at least that's valid code, thanks @Barmar –  Aug 26 '19 at 21:41
  • 1
    @tim I'm sorry, this question was totally unclear. Projects using Laravel use a PHP REPL (PsySH, rebranded as "tinker"). Sometimes I forget that other PHP developers don't use a REPL too often. So any *expression* gets, in essence, echo'd but it's actually slightly different. – Nathaniel Pisarski Aug 27 '19 at 13:03

1 Answers1

5

The statement consists of three parts: 1., . and .2. The first one evaluates to the number 1, the second one is the string concatenation operator, and the latter one evaluates to 0.2. Thus, you get 10.2.

Equivalent example code:

$a = 1.;
$b = .2;
echo "a = $a\n";
echo "b = $b\n";
echo "a.b = ".($a.$b)."\n";

outputs

a = 1
b = 0.2
a.b = 10.2
janw
  • 8,758
  • 11
  • 40
  • 62