Help with a simple function?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
KeeganWolf
Forum Newbie
Posts: 19
Joined: Thu May 14, 2009 3:13 pm

Help with a simple function?

Post 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?
Last edited by Benjamin on Thu May 21, 2009 10:43 am, edited 1 time in total.
Reason: Added [code=php] tags.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Help with a simple function?

Post by Mark Baker »

Learn about scope and passing parameters to functions
smstromb
Forum Newbie
Posts: 6
Joined: Thu May 21, 2009 1:42 pm

Re: Help with a simple function?

Post by smstromb »

Unless that array is global, you're going to have scope issues. Try passing the array to the function as a parameter.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Help with a simple function?

Post 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...
Post Reply