Jump to content

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 />';
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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