Call a dynamic function in php [duplicate] - php

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();

Related

How to write a Function to scan & remove similar words from a large variable using PHP? [duplicate]

This question already has answers here:
How to remove duplicate values from an array in PHP
(27 answers)
Closed 6 years ago.
I was trying to generate keywords using dynamic data & hence thought this function could be useful for others hence i have answered the question myself.
Here is the function named scan_dups:
function scan_dups($value){
$end = implode(', ',array_unique(explode(', ', $value)));
echo $end;
}
Now when using, just put all your values inside a variable and put that variable inside this function.
Eg:
$a = "a,b,c,d,e,g,f,g,g,s,d,gt,te,h,a";
echo scan_dups($a);
And the output would be:
a,b,c,d,e,g,f,s,gt,te,h

PHP pass second argument into function [duplicate]

This question already has answers here:
How to set optional parameter to default without passing it?
(4 answers)
Closed 5 years ago.
I have a function like this:
function myfunc(arg1=false, arg2=false){
//some code here......
}
If I only want to pass arg1 into the function, I can write like this myfunc("arg1 only variable"). But how can I pass only arg2 into the function?
Thank you.
try like this,
myfunc(false, $arg2);
Any of the following would be legal calls to your function:-
myfunc();
myfunc(true, true);
myfunc(true, null);
myfunc(null, true);
myfunc(null, null);

Using current variable function inside the function [duplicate]

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.

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?

How could I construct a variable name from another variable? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Construct a PHP variable name based on other variable values and static text
$value = '200';
$_200 = 'other';
How could I echo the contents of the second variable, getting its name from the first variable? So basically read the value of $value, prepend an _ and use it as a variable name.
Same as always.
echo ${'_'.$value};

Categories