What does these ... mean in this example? [duplicate] - php

This question already has answers here:
What is the meaning of three dots (...) in PHP?
(9 answers)
Closed 5 years ago.
I'm learning PHP http://php.net/manual/en/migration70.new-features.php and in the following example, I don't understand ... prepended with the $ints parameter in the function definition.
<?php
// Coercive mode
function sumOfInts(int ...$ints)
{
return array_sum($ints);
}
var_dump(sumOfInts(2, '3', 4.1));
Can anybody please tell me what those dots are for?
Thanks.

that means that when you call that function, you can pass X integers and the function will process them, doesn't matter how many are they. If you call sumOfInts(3,4,6,2,9) or sumOfInts(3,2,9), the function works, no matter how many arguments you pass

Related

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

This question already has answers here:
PHP | What are three dots before a function arguments?
(1 answer)
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 3 years ago.
What is the real purpose of function with ellipsis in its parameter?
I have this function:
class Dog{
public function type(...$numbers){
var_dump($numbers);
}
}
and this function
class Dog{
public function type($numbers){
var_dump($numbers);
}
}
Whether I put ellipsis or not, if I call the type function putting multiple parameters in it, its type will always be an array.
So my question is, 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.

can I create a function without brackets? [duplicate]

This question already has answers here:
Can I create a PHP function that I can call without parentheses?
(6 answers)
Closed 6 years ago.
all php functions need () in the end. However, exit doesnt need that.
Can I create a function manually, which I can later execute without () ?
Even more, If I have full access to php installation?
p.s. please dont tell me answers "exit is not function" or etc (My question is not if "exit" is function or not). I want to know HOW TO ACHIEVE like that.
No you can't. You have to edit Base of PHP language to accomplish this.
exit , echo , print and etc are not function .

What does prepending '&' to a function name mean in PHP? [duplicate]

This question already has answers here:
What does it mean to start a PHP function with an ampersand?
(3 answers)
Closed 7 years ago.
I'm using a CMS package written in PHP. In one of it's core files I saw following line that is for defining a function in a class body.
public static function &getLib($sClass, $aParams = array()) {
// Code
}
I didn't understand why the function name 'getLib' has been prepended with the ampersand(&) sign? I've never seen such thing before.
Can someone please explain me in detail why such thing has been done and what's the benefit it has over simply using the function name?
It means the function should return a reference to a variable rather than just the value itself.

'&' sign before function name in PHP [duplicate]

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 8 years ago.
Can you please explain to me the differences between two functions:
function &a(){
return something;
}
and
function b(){
return something;
}
Thanks!
The first returns a reference to something, the second a copy of something.
In first case, when the caller modify the returned value, something will be modified as a global variable do.
In the second case, modifying a copy as no effect to the source.
An ampersand before a function name means the function will return a reference to a variable instead of the value.
According to this LINK
Returning by reference is useful when you want to use a function to find to which
variable a reference should be bound. Do not use return-by-reference to increase
performance. The engine will automatically optimize this on its own. Only return
references when you have a valid technical reason to do so.

php function variable arguments [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to pass variable number of arguments to a PHP function
I make my framework in php
I want to have variable function arguments
ex
if I have 2 parameter
$a,$b;
the function become
function name($a,$b);
and if I have 3 parameter
$a,$b,$c;
the function become
function name($a,$b,$c);
Check out http://www.php.net/manual/en/functions.arguments.php#functions.variable-arg-list. Is that what you're looking for? If not, can you just accept an array?

Categories