-1

This is PHP 5.4 code...

<?php
function abc($YesNo){return $YesNo["value"];}
$YesNo = array("value"=>"No","text"=>"No");
$x = array("active"=>function(){return abc($YesNo);});
echo $x['active']();
?>

Notice: Undefined variable: YesNo on line 7
Output Should be : Yes

if i directly put array in code by replace $YesNo like

<?php
function abc($YesNo){return $YesNo["value"];}
$x = array("active"=>function(){return abc(array("value"=>"Yes","text"=>"Yes"));});
echo $x['active']();
?>

output : Yes
which is correct output. Now what's the problem in first code. I need that for re-usability

Wasim A.
  • 9,660
  • 22
  • 90
  • 120

4 Answers4

2

Try this,

You can use use for passing data to a closure.

<?php
function abc($YesNo){return $YesNo["value"];}
$YesNo = array("value"=>"No","text"=>"No");
$x = array("active"=>function() use ($YesNo) {return abc($YesNo);});
echo $x['active']();
?>
Nikhil Mohan
  • 891
  • 6
  • 13
  • o mine, i will hate anonymous function if they are handle like this use($YesNo)... what a strange syntax – Wasim A. Jun 13 '13 at 17:21
1

You provide your anonymous function with a parameter:

$x = array("active"=>function($param){return abc($param);});

then you call it:

echo $x['active']($YesNo);

You may use the use keyword to make your function aware of an external variable:

$x = array("active"=>function() use ($YesNo) {return abc($YesNo);});

but it would be quite against the idea of reusability, in this case.

moonwave99
  • 21,957
  • 3
  • 43
  • 64
1

The problem is that your variable is not accessible within the function due to Variable Scope.

Because the array is defined outside of the function, it is not by default available inside the function.

There's a couple of solutions

Disclaimer: These are intended to fit within the scope of the question. I understand that they are not necessarily best practice, which would require a larger discussion

First Option: You can declare the array within the function, like below. This is useful if you don't need access to it outside of the function.

function abc($YesNo){
    $YesNo = array("value"=>"No","text"=>"No");
    return $YesNo["value"];
}

Second Option: Within your abc function, you can add the line global $YesNo. This is useful if you do need access to the array outside of the function:

function abc($YesNo){
    global $YesNo;
    return $YesNo["value"];
}

Other options exist (such as moonwave99's answer).

Finally: Why are you putting an anonymous function within the array of $x? Seems like a path that will lead to problems down the road....

random_user_name
  • 25,694
  • 7
  • 76
  • 115
1

Your variable $YesNo needs to be visible in the scope of your anonymous function. You need to add global $YesNo as the first statement in that function:

So

$x = array("active"=>function(){return abc($YesNo);});

becomes

$x = array("active"=>function(){global $YesNo; return abc($YesNo);});

... also "value"=>"No" should be "value"=>"Yes" if you want it to return "Yes"

Precastic
  • 3,742
  • 1
  • 24
  • 29