What Is the Meaning of Three Dots (...) in PHP

What is the meaning of three dots (...) in PHP?

This is literally called the ... operator in PHP, but is known as the splat operator from other languages. From a 2014 LornaJane blog post on the feature:

This feature allows you to capture a variable number of arguments to a function, combined with "normal" arguments passed in if you like. It's easiest to see with an example:

function concatenate($transform, ...$strings) {
$string = '';
foreach($strings as $piece) {
$string .= $piece;
}
return($transform($string)); }

echo concatenate("strtoupper", "I'd ", "like ", 4 + 2, " apples");

(This would print I'D LIKE 6 APPLES)

The parameters list in the function declaration has the ... operator in it, and it basically means " ... and everything else should go into $strings". You can pass 2 or more arguments into this function and the second and subsequent ones will be added to the $strings array, ready to be used.

What do the three dots before a function argument represent?

It indicates that there may be a variable number of arguments.

When the function is called with more than 3 arguments, all the arguments after $next will be added to the $guards array.

You can read about it here.

Meaning of Triple Dots [...] in Flutter Syntax

Dart 2.3 introduced the spread operator (...) and the null-aware spread operator (...?), which provide a concise way to insert multiple elements into a collection.

For example, you can use the spread operator (...) to insert all the elements of a list into another list:

var list = [1, 2, 3];
var list2 = [0, ...list];
assert(list2.length == 4);

If the expression to the right of the spread operator might be null, you can avoid exceptions by using a null-aware spread operator (...?):

var list;
var list2 = [0, ...?list];
assert(list2.length == 1);

For more details and examples of using the spread operator, see the spread operator proposal.

Add ... if string is too long PHP

The PHP way of doing this is simple:

$out = strlen($in) > 50 ? substr($in,0,50)."..." : $in;

But you can achieve a much nicer effect with this CSS:

.ellipsis {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}

Now, assuming the element has a fixed width, the browser will automatically break off and add the ... for you.

Removing the three dots from the end of the excerpt

Hello you have to add one css for that in child theme style.css

you found css in modules.min.css file

.eltd-post-excerpt:after {
position: relative;
content: '...';
}

so you have just override that css with this given css and dots are remove.

.eltd-post-excerpt:after {
content: ' ';
}

Hope this work as you want.

Thank You

How to make PHP understand *either* an array or the splat operator (3 dots ...) for function parameters?

It has different results in the function.

1) If you pass multiple strings to the function, it will get an array of strings like so:

test('a', 'series of', 'strings go here');

//vardump result
array (size=3)
0 => string 'a' (length=1)
1 => string 'series of' (length=9)
2 => string 'strings go here' (length=15)

2) If you pass an array to function it will get an array of arrays. The splat operator will add a first entry that you pass to function to the first index of an array, the second parameter will add into the second index and so on. so no matter what you pass to the function (array or string) it will go to index zero. but the problem is if you pass array it will generate an array of arrays in the first index of the array:

test([ 'a', 'series of', 'strings go here' ]);

//vardump result
array (size=1)
0 =>
array (size=3)
0 => string 'a' (length=1)
1 => string 'series of' (length=9)
2 => string 'strings go here' (length=15)

Conclusion:

you can solve this with one of these two ways:

1) put 3 dots before you pass the array to the function:

test(...[ 'a', 'series of', 'strings go here' ]);

2) Another way of doing this is by adding a function named is_multi_array() to check if the variable that passed to the function is multi-dimensional. After that you can simply get the first element of the array and put it in strings variable:

function is_multi_array( $arr ) {
rsort( $arr );
return (isset( $arr[0] ) && is_array( $arr[0] ));
}

function test(...$strings)
{
if(is_multi_array($strings)){
$strings = $strings[0];
}

//do the rest
}

This way you can use the function in both ways that you like:

test('a', 'series of', 'strings go here');
test([ 'a', 'series of', 'strings go here' ]);

Three dots menu PHP HTML

If you remove any duplicated IDs from the HTML and modify the 3-dots portion like so

<div class='dropdownPosts'>
<!-- three dots -->
<ul class='dropdownbtn icons btn-right showLeft' onclick='showDropdown(event)'>
<li></li>
<li></li>
<li></li>
</ul>
<!-- menu -->
<div class='dropdownPost-content'>
<a href='#home'>Home</a>
<a href='#about'>About</a>
<a href='#contact'>Contact</a>
</div>
</div>

You can then likey modify your showDropdown function like this:

function showDropdown(event) {
event.target.nextElementSibling.classList.toggle("show");
}

why exactly should I put the ellipsis inside the parameter of a function?

It's just syntactic sugar, called variable-length argument lists. It lets you pass the function multiple arguments that it will turn into an array automatically. In that example, it would let you call type(1, 2, 3) and $numbers would be an array of those three numbers.

... in php function parameters

This is a special Unary operator of PHP called "Spread operator" which allows you to have variable-length parameters in a method i.e. the method can take 0 or more parameters for the variable-length parameter.

Below is an example for better understanding:

E.g.

<?php
function sum(...$numbers) {
$acc = 0;
foreach ($numbers as $n) {
$acc += $n;
}
return $acc;
}

echo "\n".sum(1, 2, 3, 4);
echo "\n".sum(1);
echo "\n".sum();

Output:

10
1
0

Reference: https://www.php.net/manual/en/functions.arguments.php#functions.variable-arg-list



Related Topics



Leave a reply



Submit