Page 1 of 1

Help with a simple function?

Posted: Thu May 21, 2009 9:35 am
by KeeganWolf
I'm trying to create a function I can use inside a loop to sort information when it reaches a certain point in an array. :banghead:
This doesn't seem to echo when needed...

Code: Select all

 
function yburg(){
    $num = intval($data[$j][2]);
    if ($num == 5){
    echo "The PLU number 5 is a Cheesburger";}
}
for ($j = 0; $j < $count2; $j++) {
    yburg();
    echo $data[$j][2], $data[$j][3], '<br />';
}
 
Where as this does...

Code: Select all

 
for ($j = 0; $j < $count2; $j++) {
        echo $data[$j][2], $data[$j][3], '<br />';
        $num = intval($data[$j][2]);
    if ($num == 5){
    echo "The PLU number 5 is a Cheesburger"}
}
 
During the loop $data[$j][2] goes from 1 to 144..
Any help for a newcomer to php?

Re: Help with a simple function?

Posted: Thu May 21, 2009 12:00 pm
by Mark Baker
Learn about scope and passing parameters to functions

Re: Help with a simple function?

Posted: Thu May 21, 2009 1:54 pm
by smstromb
Unless that array is global, you're going to have scope issues. Try passing the array to the function as a parameter.

Re: Help with a simple function?

Posted: Thu May 21, 2009 2:09 pm
by requinix
smstromb wrote:Unless that array is global, you're going to have scope issues. Try passing the array to the function as a parameter.
Hmm, deja-vu...