Jump to content

Simple question about function in loop


KeeganWolf

Recommended Posts

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

 

  1. 

  2. function yburg(){

  3.    $num = intval($data[$j][2]);

  4.    if ($num == 5){

  5.    echo "The PLU number 5 is a Cheesburger";}

  6. }

  7. for ($j = 0; $j < $count2; $j++) {

  8.    yburg();

  9.    echo $data[$j][2], $data[$j][3], '<br />';

  10. }

  11. 

 

 

Where as this does...

 

  1. 

  2. for ($j = 0; $j < $count2; $j++) {

  3.        echo $data[$j][2], $data[$j][3], '<br />';

  4.        $num = intval($data[$j][2]);

  5.    if ($num == 5){

  6.    echo "The PLU number 5 is a Cheesburger"}

  7. }

  8. 

 

 

During the loop $data[$j][2] goes from 1 to 144..

Any help for a newcomer to php?

Link to comment
https://forums.phpfreaks.com/topic/159124-simple-question-about-function-in-loop/
Share on other sites

You should pass $data[$j][2] to your function when you call it.

 

function yburg($num)
{
     if (intval($num) == 5)
        echo "The PLU number 5 is a Cheesburger";
}

for ($j = 0; $j < $count2; $j++)
{
    yburg($data[$j][2]);

    echo $data[$j][2], $data[$j][3], '<br />';
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.