Ernie All American 45943 Posts user info edit post |
My PHP knowledge is limited, so I very well could be doing this ass-backwards
I 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; } } }
This gives me an error that an invalid argument is supplied for the foreach loop. Passing $crap to peepee() gives me an error message that I am missing one argument for peepee(). I would think that defining $crap as a global variable would at least be a step in the right direction, but that does nothing for me.
[Edited on March 13, 2008 at 10:23 PM. Reason : ]3/13/2008 10:17:24 PM |
mienutzich All American 4300 Posts user info edit post |
$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; } } }
donno if this is how you were doing it before...
if crap is not set outside of the parent function and included as a global variable and then included again as a global in the child then it will not work3/13/2008 10:54:26 PM |
Ernie All American 45943 Posts user info edit post |
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 |
DirtyMonkey All American 4269 Posts user info edit post |
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; } }
that way you know that $crap is always set, even if it's an empty array. if you ever decide to set $crap from a parameter of doodoo() - which would eliminate the need for that if/else block - you might want to wrap the foreach loop with if(is_array($crap)) to make sure the foreach won't fail.
p.s. you have dirty variable naming conventions 3/14/2008 2:18:06 AM |
Ernie All American 45943 Posts user info edit post |
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 |
Stein All American 19842 Posts user info edit post |
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 |
Ernie All American 45943 Posts user info edit post |
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 |
Talage All American 5093 Posts user info edit post |
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 |
Ernie All American 45943 Posts user info edit post |
And I would love to post the other 2000 lines of code that would clarify the matter 3/14/2008 5:58:10 PM |