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.
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:
Use a variable to define a PHP function
(1 answer)
How to call a function from a string stored in a variable?
(18 answers)
Closed 5 years ago.
Is there a way to call dynamic functions in php. For an example lets guess I have a variable like below.
$myVar = 'my_test_function';
And I have a function named myTestFunction(). Is there a way to call the mentioned function using the $myVar variable's value? How to structure that variable as myTestFunction and call the mentioned function using that variable
Just do the following:
lcfirst(str_replace('_', '', ucwords($myVar, '_')))();
convert your string to function name first:
$myVar = 'my_test_function';
echo $myVar = str_replace('_', '', ucwords($myVar, '_')); //output myTestFunction
$myVar();
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.
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?
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 know & is used to create references.
But I wonder what having a & before a function name means:
function &DB($params = '', $active_record_override = FALSE) { // code }
It returns the result by reference. See the manual entry for it here.