I would like to do something like this, but... working!
function _mathOperation($a, $b, $operation, $conversion)
{
return $operation($conversion($a), $conversion($b));
}
echo _mathOperation(4, 5, function($a, $b) { return $a+$b; }, intval);
echo _mathOperation(4.6, 5, function($a, $b) { return $a+$b; }, floatval);
I have the problem with passing intval and floatval as function parameter.
How do you do handle this?
you were missing the quotes around the function names:
function _mathOperation($a, $b, $operation, $conversion)
{
return $operation($conversion($a), $conversion($b));
}
echo _mathOperation(4, 5, function($a, $b) { return $a+$b; }, "intval");
// ^^ ^^
echo _mathOperation(4.6, 5, function($a, $b) { return $a+$b; }, "floatval");
// ^^ ^^
http://3v4l.org/AYiKN
You should pass the conversion parameter as the function name, rather than trying to pass the function.
e.g.
<?php
echo _mathOperation(4, 5, function($a, $b) { return $a+$b; }, 'intval');
Related
I'm try to solve a task which uses new functions php7 uniform variable syntax nested () support foo()() (https://wiki.php.net/rfc/uniform_variable_syntax).
I need write function test for this code:
$sum = function($a, $b) { return $a + $b; };
test(6)(2)(3)($sum); // 11
test(3)(1)($sum); // 4
test(3)(3)('pow'); // 27
I don't found any explanation for this feature. Where can I find how to use it? I see that I must return function name in function test, but how to pass argument?
Thanks all for help. It's something like this:
<?php
function test($a) {
echo '<br/>';
$arr[] = $a;
return $mf = function($b) use(&$mf, &$a, &$arr) {
if(gettype($b) == 'object') {
echo(array_reduce($arr, $b));
} elseif (gettype($b) == 'string') {
if($b == 'pow') {
echo array_reduce($arr, function ($carry, $a) {
return !empty($carry) ? pow($carry, $a) : $a;
});
}
} elseif (gettype($b) == 'integer') {
$arr[] = $b;
}
return $mf;
};
}
$sum = function($a, $b) { return $a + $b; };
test(6)(2)(3)($sum); // 11
test(3)(1)($sum); // 4
test(3)(3)('pow'); // 27
This is more about nested recursive functions, or currying, than that rfc. That rfc just enabled the syntax that supported it.
This uses recursion until you pass a callable:
function test($var) {
$values = [$var];
$function = function($callback) use (&$values, &$function) {
if (is_callable($callback)) {
return array_reduce(array_slice($values, 1), $callback, $values[0]);
}
$values[] = $callback;
return $function;
};
return $function;
}
Because your functions expect two parameters but your nesting could have unlimited parameters, it's best to use an array and array reduce.
However, since multiplication functions like pow won't work with a null initial value, you can specify the initial value as the first passed parameter from the array.
What's the syntax of calling a function in another function in php?
I want something like:
function argfunction($a,$b,$c){
}
function anotherfunction(argfunction($a,$b,$c), $d, $e)
{
}
I am not calling argfunction again in the definition of anotherfunction
The parameters of a function should be declarative, i.e. they are not supposed to do something.
But you can do this with the callable keyword (PHP 5.4):
function argfunction($a,$b,$c){
return $a+$b+$c;
}
function anotherfunction(callable $a_func, $a, $b, $c, $d, $e) {
// call the function we are given:
$abc = $a_func($a, $b, $c);
return $abc + $d * $e;
}
// call:
anotherfunction ("argfunction", 1, 2, 3, 4, 5); // output: 26
Or you can pass the whole function definition:
echo anotherfunction (function ($a, $b, $c) {
return $a+$b+$c;
}, 1, 2, 3, 4, 5); // output: 26
Or, assign a function to a variable, and pass that:
$myfunc = function ($a, $b, $c) {
return $a+$b+$c;
};
echo anotherfunction ($myfunc, 1, 2, 3, 4, 5); // output: 26
But if you just want to pass the result of a function call to another function, then it is much more straightforward:
function anotherfunction2($abc, $d, $e) {
return $abc + $d * $e;
}
echo anotherfunction2 (argfunction(1, 2, 3), 4, 5); // output: 26
Does not make sense but I will assume that you are expressing your idea in a wrong way.
Would you maybe looking for something similar to callback?
Take a look at the following: here and here
My code:
function sortx($a, $b) {
if(!strpos($a["p_title"],'apple ipad')) {
return -1;
}
return 1;
}
usort($arr, 'sortx');`
In above function I want to pass:
$sort_text='apple ipad';
, when calling function instead of hardcoding apple ipad into strpos(). How can I accomplish that?
Call it with a closure:
$sort_text='apple ipad';
usort(
$arr,
function ($a, $b) use ($sort_text) {
if(!strpos($a["p_title"], $sort_text)) {
return -1;
}
return 1;
}
);
and you can pass additional arguments with the use clause
Is it possible to pass an operator to a function? Like this:
function operation($a, $b, $operator = +) {
return $a ($operator) $b;
}
I know I could do this by passing $operator as a string and use switch { case '+':... }. But I was just curious.
It's not possible to overload operators in php, but there is a workaround. You could e.g. pass the functions add, sub, mul and etc.
function add($a, $b) { return $a+$b; }
function sub($a, $b) { return $a-$b; }
function mul($a, $b) { return $a*$b; }
And then you function would be something like:
function operation($a, $b, $operator = add) {
return $operator($a, $b);
}
This can be done using eval function as
function calculate($a,$b,$operator)
{
eval("echo $a $operator $b ;");
}
calculate(5,6,"*");
Thanks.
Try, You cannot able to pass the operators in functions, YOU CAN USE FUNCTION NAME LIKE ADDITION, SUBTRACTION, MULTIPLICATION ... etc,
function operation($a, $b, $operator ='ADDITION') {
$operator($a, $b);
}
function ADDITION($a, $b){
return $a + $b;
}
I know you specifically mention not using a switch statement here, but I think it's important to show how this could be set up using switch statements as in my opinion it is the easiest, safest, and most convenient way to do this.
function calculate($a, $b, $operator) {
switch($operator) {
case "+":
return $a+$b;
case "-":
return $a-$b;
case "*":
return $a*$b;
case "/":
return $a/$b;
default:
//handle unrecognized operators
}
return false;
}
This usort function is returning the array reverse from what I want. It's returning an array like ("1", "2", "3"). How can I make it return ("3", "2", "1")?
usort($myArray, function($a, $b) {
return $a["comments"] - $b["comments"];
});
Just reverse the parameters?
usort($myArray, function($a, $b) {
return $b["comments"] - $a["comments"];
});
usort($myArray, function($a, $b) {
return $b["comments"] - $a["comments"];
});
Just change A to B and B to A.
you can reverse your function outputs.
usort($myArray, function($a, $b) {
return $b["comments"] - $a["comments"];
});
usort($myArray, function($a, $b) {
if($a['comments'] === $b['comments']) {
return 0;
}
return ($a['comments'] > $b['comments']) ? -1 : 1;
});
$myArray = array("1", "2", "3");
$reversed_array = array_reverse($myArray);