What happens in 1...1 in PHP?

Asked

Viewed 544 times

29

On the web I came across the following piece of code:

echo 1...1;

Resulting in 10.1. I did the var_dump of the same and the return is:

string(4) "10.1"

Works with variable assignment:

$x = 1...1;
echo $x; // 10.1

And for other values:

echo 2...2; // 20.2
echo 1...1; // 10.2
echo 3...2; // 30.2

I know that in PHP 5.6+ there is the operator ... (splat Operator), that extracts the values of a array - similar to the * Python - but it doesn’t seem to be the case, because, according to the site 3V4L, the result is the same since version 4.3.0, that there was no such operator.

Apparently it only works with integers.

Would be some implicit value concatenation that PHP does?

  • 7

    Poor thing about PHP.

  • 7

    I never cease to amaze myself with PHP :)

  • 7

    What a tragedy, every day I discover a new.

  • 1

    Crendeuspai. I’ve said it more than once and I repeat: If I wasn’t forced to work with PHP, I would have quit long ago. It seems that the pattern of things with PHP is Undefined behavior or confusing syntax.

  • 6

    As for mimimi, the answer has already clarified :p

1 answer

40


In fact, what occurs is the concatenation of values of the type float. PHP analyzes 1. as being float(1.0) and .1 as being float(0.1), in this way, the concatenation of float(1.0) . float(0.1), however, how 1.0 is whole, PHP keeps only 1.

1. . .1
|  |  |
|  |  +--- Analisado como float 0.1
|  +--- Operador de concatenação de string, faz o cast dos operandos para string
+--- Analisado como float 1.0, feito o cast para int

That is, to do:

echo 1...1;

Is the same as:

echo (1.).(.1);

So much so that it is possible to add the decimal part of the first value:

echo 5.32..7; // 5.320.7

But it is not possible to add an entire part to the second value:

echo 3..5.67; // Syntax error

Only if parentheses are properly inserted:

echo 3..(5.67); // 35.67

Or spaces between operators:

echo 3. . 5.67; // 35.67

Using space it is still possible to define both the decimal part of the first value and the entire part of the second:

echo 3.14 . 1.41; // 3.141.41

Summary of the opera

I ran a series of testing and the result is below. I did not include situations that would generate syntax error.

1...1        = 10.1
2...2        = 20.2
10...10      = 100.1
(1.).(.1)    = 10.1
1. . .1      = 10.1
5.32..7      = 5.320.7
3..(5.67)    = 35.67
3. . 5.67    = 35.67
3.14 . 1.41  = 3.141.41

According to the tests in 3V4L, the output is the same since version 4.3.0:

inserir a descrição da imagem aqui

  • 1

    Now it made sense. + 1

  • Is there any link, any official text, anything that says why it was done like this?

  • 2

    @Virgilionovic I don’t think so. PHP documentation misses a lot in these aspects.

  • Gizuis. And I thought only JS liked to be cryptic. :-(

  • 2

    I’m trying not to say "it was done like that". What happens is that it 'got like this", when making some other decisions - In this case, mainly the use of the "." as an operator to concatenate strings that automatically casts to "str". These strange corner-cases derived from rules that are very sensible when used in the right context are common in all languages.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.