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.
Related
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
This question already has answers here:
Does function definition order matter?
(7 answers)
Closed 1 year ago.
The following code runs in PHP
<?php
$foo = "Chocolate milkshake";
go($foo);
function go($param) {
echo $param;
}
?>
// Output: chocolate milkshake
See this Demo http://codepad.viper-7.com/ToApZa
This code runs without errors and prints specified output, why?
I thought this "function hoisting" only occurred in JavaScript
It doesn't matter where you declare your functions in PHP in most cases, as you've just proved :)
Take a look at this page for more details. The key point:
Functions need not be defined before they are referenced, except when a function is conditionally defined as shown in the two examples below.
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.
This question already has answers here:
Anonymous recursive PHP functions
(6 answers)
Closed 9 years ago.
i need to recursively call a variable function, how can i do this?
$rec = function($li) use($html,$rec) { // error, rec was not defined yet
if( ... ) $rec( ... );
}
how can i do this?
Use the function variable $rec by reference (&$rec) so you can set it to the function then. This will also define it.
use($html, &$rec)
^
You find this principle outlined in the question Anonymous recursive PHP functions.
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?