My PHP knowledge is limited, so I very well could be doing this ass-backwardsI have the following code in which doodoo() accepts a variable and depending on the value of the variable sets $crap[] to a particular set of values. Another function, peepee() is then created which (for the sake of simplicity) echos the values of $crap[].
function doodoo ($poop) { if ($poop == "a") { $crap = array(...); } elseif ($poop == "b") { $crap = array(...); } function peepee () { foreach ($crap as $var) { echo $var; } }}
3/13/2008 10:17:24 PM
$crap=array(..);function doodoo ($poop) { global $crap; if ($poop == "a") { $crap = array(...); } elseif ($poop == "b") { $crap = array(...); } function peepee () { global $crap; foreach ($crap as $var) { echo $var; } }}
3/13/2008 10:54:26 PM
Well, I got it working. But $crap doesn't have to be defined outside of the initial function (that would negate the entire use of the function anyway), it only needs to be defined as a global variable in each of the functions. I was only defining it in the first. Thanks for the help.
3/13/2008 11:01:06 PM
i see you've got it working, but...peepee() doesn't really need to be a function, and i'm not even sure if you can define a function inside of a function anyway. but, aside from that, $crap is ONLY defined if $poop == "a" OR $poop == "b". so if $poop = "c", $crap is never set. you should do something like this to make sure it's set.
function doodoo($poop) { $crap = array(); if($poop == "a") { $crap = array(...); } else if ($poop == "b") { $crap = array(...); } foreach($crap as $var) { echo $var; }}
3/14/2008 2:18:06 AM
You can definitely nest functions in PHP and the inner function is necessary There's a lot more going on in the actual code; I just posted the bare bones for the sake of simplicity.
3/14/2008 7:46:15 AM
The issue is that nested function or not, you still need to pass it the variable, which you weren't doing at first.
3/14/2008 8:27:15 AM
That was never really the problem. I knew the variable had to be passed for the foreach loop to run, I just didn't know how to make the foreach loop know the variable existed.
3/14/2008 8:34:36 AM
I would love to hear more details on why you're using a nested function. Just out of curiosity.
3/14/2008 4:15:50 PM
And I would love to post the other 2000 lines of code that would clarify the matter
3/14/2008 5:58:10 PM