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
Related
i am trying to use this code:
$arr = [
function ($a) {
return $a + $a;
},
function ($b) {
return $b * $b;
},
function ($c,$cc) {
return $c % $cc - $c;
},
function ($d) {
return $d + 4 / $d;
}
];
how can i pass parameters to this functions?
i have already learned this statment for function with no arguments. for example:
function () { echo 'somethings'; } that's mean this function in an array don't have incoming arguments.
$test = rand (0,1);
echo $myarr[$test]();
In addition
can i change (rand) statment with other statment in the above code snippet?
You can do the following:
Your array:
$arr = [
function ($a) {
return $a + $a;
},
function ($b) {
return $b * $b;
},
function ($c,$cc) {
return $c % $cc - $c;
},
function ($d) {
return $d + 4 / $d;
}
];
Your rand
$test = rand(0,1);
---- note ----
you can change your rand() to any other statement provided that it an existing index of the $arr array. in order to know if $test value is an index which exists in the $arr array, you can do
if (isset($arr[$test])) {}
---- end note ---
to call the functions
$returnedValue = call_user_func($arr[$test], 10);
echo $returnedValue;
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');
I was wondering if I can, in PHP, like JavaScript, apply a function call to the return of an object. In JavaScript I can do this:
var y = 1;
var x = y.toString().concat(' + 1');
console.log(x);
And I think if it is possible to do almost the same in PHP. I was thinking in recursion to this and I didn't know exactly the name to search for it. I'm trying, in the moment:
<?php
class Main {
public function __construct() {
$this->Main = new Main;
}
public function merge(/* this */ $xs, $ys) {
return array_merge($xs, $ys);
}
public function add(/* this */ $xs, $ys) {
return array_push($xs, $ys);
}
}
$aux = new Main;
$x = $aux -> merge([1, 2, 3], [4, 5, 6])
-> add(7)
-> add(8)
-> add(9);
// $x => [1, 2, 3, 4, 5, 6, 7, 8, 9]
?>
This is overflowing everything. I receive an overflow message:
Maximum function nesting level of '100' reached
Am I able to do this, like in JavaScript? Almost the same as C# extension methods.
Its called method chaining:
class Main {
private $ar = array();
public function merge($xs, $ys) {
$this->ar = array_merge($xs, $ys);
return $this;
}
public function add($ys) {
$this->ar[]= $ys;
return $this;
}
public function toArray(){
return $this->ar;
}
//if you want to echo a string representation
public function __toString(){
return implode(',', $this-ar);
}
}
$aux = new Main;
$x = $aux->merge([1, 2, 3], [4, 5, 6])->add(7)->add(8)-> add(9);
echo $x;
var_dump($x->toArray());
You can work with the returns from functions, but it’s not as nice-looking as JavaScript:
<?php
$array1 = array(1, 2, 3);
$array2 = array(4, 5, 6);
$x = array_push(array_push(array_push(array_merge($array1, $array2), 7), 8), 9);
$x should then be an array containing the numbers 1 through 9.
Is it possible to have an array and pass it into a function as separate arguments?
$name = array('test', 'dog', 'cat');
$name = implode(',' $name);
randomThing($name);
function randomThing($args) {
$args = func_get_args();
// Would be 'test', 'dog', 'cat'
print_r($args);
}
No. That's what call_user_func_array() is for.
As of PHP 5.6 you can use ... to pass an array as function arguments. See this example from the PHP documentation:
function add($a, $b) {
return $a + $b;
}
echo add(...[1, 2])."\n";
$a = [1, 2];
echo add(...$a);
Can a PHP function return a lot of vars without array?
I want like that:
<?php
public function a()
{
$a = "a";
$b = "b";
$c = "c";
}
echo a()->a;
echo a()->b;
echo a()->c;
?>
How can I access to $a,$b,$c vars?
Instead of an array, you could use an associative array and cast it to an object, which allows you to access the elements using the -> object syntax:
function a()
{
return (object) array(
'a' => "a",
'b' => "b",
'c' => "c");
}
echo a()->a; // be aware that you are calling a() three times here
echo a()->b;
echo a()->c;
function a1() {
return array(
'a' => 'a',
'b' => 'b',
'c' => 'c'
);
}
$a1 = a1();
echo $a1['a'];
echo $a1['b'];
echo $a1['c'];
function a2() {
$result = new stdClass();
$result->a = "a";
$result->b = "b";
$result->c = "c";
return $result;
}
$a2 = a2();
echo $a2->a;
echo $a2->b;
echo $a2->c;
// or - but that'll result in three function calls! So I don't think you really want this.
echo a2()->a;
echo a2()->b;
echo a2()->c;
http://php.net/manual/en/function.list.php
Create a class that holds your 3 variables and return an instance of the class. Example:
<?php
class A {
public $a;
public $b;
public $c;
public function __construct($a, $b, $c) {
$this->a = $a;
$this->b = $b;
$this->c = $c;
}
}
function a() {
return new A("a", "b", "c");
}
echo a()->a;
echo a()->b;
echo a()->c;
?>
Of course the last 3 lines are not particularly efficient because a() gets called 3 times. A sensible refactoring would result in those 3 lines being changed to:
$a = a();
echo $a->a;
echo $a->b;
echo $a->c;
If you want to access those variables that way (without using an array), you should better use a class:
class Name{
var $a = "a";
var $b = "b";
}
$obj = new Name();
echo $obj->a;
You could build a class and return an object instead. Check out this SO answer for something that you might find useful.
Yes this is possible when the return variable has to be as an array or it should be long string with multiple values separated with any of the delimiters like ", | #" and so on.
if it is built as an array we can receive it using var_dump function available in PHP
see below code
var_dump($array);
PHP Manual > Returning values:
A function can not return multiple values, but similar results can be obtained by returning an array.
<?php
function small_numbers()
{
return array (0, 1, 2);
}
list ($zero, $one, $two) = small_numbers();
no need for classes here.