Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does splat operator between two integers mean?

Tags:

php

What does splat operator between two integers mean?

As far as I know, the splat operator is used for passing the parameter on function. Manual.

An example :

<?php
function fx(...$a) {
 print_r($a);
}
fx(1, 2, 3, 4, 5, 6); // array 1, 2, 3, 4, 5, 6

But what does it mean when the splat operator is between two integers like this?

<?php
echo 1...3; // Why is the output 10.3?
echo PHP_EOL;

echo 12...100; // Why is the output 120.1?
echo PHP_EOL;
like image 554
Ammar Faizi Avatar asked Nov 28 '25 01:11

Ammar Faizi


1 Answers

... operator works only for unpacking traversable objects.

Your example uses . operator to concatenate two numbers interpreted as strings.

The following are equivalent:

echo 1...3;         // interpreted as ((int) 1) . ((float) 0.3)
echo '1' . '0.3';

echo 12...100;      // interpreted as ((int) 12) . ((float) 0.1)
echo '12' . '0.1';
like image 167
Joe Avatar answered Nov 29 '25 14:11

Joe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!