13

I have the following code snippet where I don't really understand its output:

echo 20...7;

Why does this code output 200.7?

From what I know ... is the splat operator, which it is called in ruby, that lets you have a function with a variable number of arguments, but I don't understand what it does here in the context with echo.

Can anyone explain what exactly this code does?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Daniel Bejan
  • 1,468
  • 1
  • 15
  • 39
  • [... Argument unpacking (since PHP 5.6)](http://wiki.php.net/rfc/argument_unpacking) [as per contained](http://stackoverflow.com/q/3737139/1415724) in the duplicate. – Funk Forty Niner May 17 '17 at 11:40
  • you two guys in the same class? http://stackoverflow.com/q/44023880/1415724 – Funk Forty Niner May 17 '17 at 11:45
  • @JohnConde am starting to wonder if I did a bad close – Funk Forty Niner May 17 '17 at 11:45
  • @JohnConde seeing that you closed http://stackoverflow.com/q/44023880/1415724 I guess I just answered my own question *lol* – Funk Forty Niner May 17 '17 at 11:46
  • I don't see how that is relevant to my question @Fred-ii- or how do you apply that to `echo`? It's not a homework assignment or anything, just a piece of code I found acting weird and wanting to understand it – Daniel Bejan May 17 '17 at 11:57
  • @Fred-ii- nothing to do with unpacking as you suggested otherwise. This is simply float to string conversions playing with string concatenation. 20 comes out as 20 and .7 comes out at `0.7`. so you got `20`.`0.7` – YvesLeBorg May 17 '17 at 12:00
  • @Fred-ii- Not sure if the duplicate is a very good fit for this one, since the formatting of the code can probably confuse some people and that confusion isn't cleared in the duplicate. To explain to OP what is happening here: Basically you concatenate two numbers. If you write your code a bit different it looks like this: `echo 20. . .7`. Now you have to know that you don't have to write the 0 in PHP if you want to write `0.7` you can omit that. So basically you do this: `echo 20 . 0.7`. /1 – Rizier123 May 17 '17 at 12:00
  • And while concatenating those two numbers PHP will silently convert them to a string and basically append `"0.7"` to `"20"` which result in `"200.7"`. /2 – Rizier123 May 17 '17 at 12:01
  • 1
    @Rizier123 I reopened it. – Funk Forty Niner May 17 '17 at 12:01
  • @Rizier123 Side note: real funny though how another question earlier which is now deleted http://stackoverflow.com/q/44023880/1415724 was posted though. I doubt that was a coincidence. – Funk Forty Niner May 17 '17 at 12:02
  • @Fred-ii- Yes I saw that one too :) Closing the second one as duplicate of the earlier question here would probably have been better, but OP deleted the question already. – Rizier123 May 17 '17 at 12:04
  • @Rizier123 Shadow puppet syndrome; not much welcome, IMHO. – Funk Forty Niner May 17 '17 at 12:05
  • @YvesLeBorg The question is reopened and since you explained it first in the comments you can write a good answer if you want and explaining what is going on with this little code snippet. – Rizier123 May 17 '17 at 12:05
  • And why does `echo 20..7` generate an error and `echo 20...7` not? Does php see them as `echo 20.[0] . [0].7;` ? – Daniel Bejan May 17 '17 at 12:08
  • I don't get why `12..6` doesn't output, I expected that to be interpreted as `12` concat `.6`? – Martin May 17 '17 at 12:13
  • 1
    Maybe the confusion of the 2 post being the same was because I posted the same question on a facebook it group but no one was able to answer it – Daniel Bejan May 17 '17 at 12:13
  • @csanonymus Yes PHP does exactly this: `echo 20.[0] . [0].7` YvesLeBorg explained it already in his comment and I also explained it in mine above. – Rizier123 May 17 '17 at 12:14
  • 1
    @Martin PHP interprets `12..6` as `12. .6` which basically is `12 0.6` and since you don't have any operator in between those two numbers PHP does not know what you want to do. – Rizier123 May 17 '17 at 12:15
  • @Rizier123 php has a splat operator too, not just Ruby http://php.net/manual/ro/migration56.new-features.php – Daniel Bejan May 17 '17 at 12:16
  • @csanonymus I know, but PHP does not call it splat operator, that is why I explicitly wrote it in your question, so people don't get confused. – Rizier123 May 17 '17 at 12:17
  • @Rizier123 I made an assumption that PHP was dealing with these figures as integers in the first instance but clearly that's not so, they're always going to be floats with `.0` parts, Cheers – Martin May 17 '17 at 12:18
  • @Rizier123 this raises another question : should this be viewed as a bug on PHP ie. should not the splat operator be parsed atomically, prior to any other manipulation? – YvesLeBorg May 17 '17 at 12:22
  • Haha, @YvesLeBorg that's what I've been thinking too.. Is this a bug or a feature after all? – Daniel Bejan May 17 '17 at 12:27
  • @YvesLeBorg Not a bug. Since the `...` would need to be before the function argument and the argument itself needs to be a traversable, it can't be function unpacking. – Rizier123 May 17 '17 at 12:32
  • PHP is not Ruby – GordonM May 17 '17 at 13:20
  • @Rizier123 right .... throw TooManyLanguagesExceptions; yield asm; ps, nice edit on the answer :) – YvesLeBorg May 17 '17 at 14:43

1 Answers1

14

No this is not the splat/unpacking operator, even thought it might seem like it is. This is just the result of the PHP parsing process. Already writing your code a bit different might clear some confusion:

echo  20.           .           .7;
#       ↑           ↑           ↑
#    decimal  concatenation  decimal
#      dot         dot         dot

Now you have to know that .7 is 0.7 and you can omit the 0 in PHP as described in the syntax for float numbers:

DNUM          ([0-9]*[\.]{LNUM}) | ({LNUM}[\.][0-9]*)

So PHP just concatenates those two numbers together and while doing this PHP's type juggling will silently convert both numbers to strings.

So in the end your code is equivalent to:

echo "20" . "0.7";
//Output: "200.7"
Rizier123
  • 58,877
  • 16
  • 101
  • 156
YvesLeBorg
  • 9,070
  • 8
  • 35
  • 48