0

I have a following function in php:

function addEntryHere($day, $numberId, $numberOfTexts, $entries) {

    foreach($entries as $entry) {
        if($entry->$day == $day) {
            $entry->addToEntry($numberId, $numberOfTexts);
        }
    }
    $newEntry = new Entry($day);
    $newEntry->addToEntry($standId, $numberOfTexts);

    array_push($entries, $newEntry);

    //print_r($entries);
}

and when I invoke this function in a loop:

while($row = mysqli_fetch_array($result)) {
    echo "count table before: " . count($entries);
    for($i=23; $i<26; $i++) {
        addEntryHere($i, $row[1], $row[2], $entries);
        //print_r($entries);
    }
    echo "count table after: " . count($entries);
}

I see only:

count table before: 0
count table after: 0

My addToEntry method is quite simple:

function addToEntry($numberId, $numberOfTexts) {
        switch($numberId) {
            case 1: $this->number1+= $numberOfTexts; break;
            case 2: $this->number2+= $numberOfTexts; break;
        }
    }

So why do I get constantly the output 0, even though there is some data in the $result? I've decided to pass the array $entries to the addEntryHere method because I couldn't refer to it in the method, even though I thought it has a global scope...

======= EDIT

after following the watcher's suggestion I modified my code, but then this line:

if($entry->$day == $day) {

throws me the error:

Notice: Undefined property: Entry::$23 

and the browser prints such errors many, many times (since it's in the while loop). What might be the problem here?

user3766930
  • 5,629
  • 10
  • 51
  • 104

1 Answers1

1

You are only modifying the local variable inside the function. After every invocation of the function, that local variable disappears and is lost. What you want to do is return the value of your computation back to the calling context so that it can be used later:

    array_push($entries, $newEntry);

    return $entries;
}

And the call:

while($row = mysqli_fetch_array($result)) {
    echo "count table before: " . count($entries);
    for($i=23; $i<26; $i++) {
        $entries = addEntryHere($i, $row[1], $row[2], $entries);
        //print_r($entries);
    }
    echo "count table after: " . count($entries);
}

If you're trying to work with the global scope, note that in PHP you have to explicitly import the global scope inside of the function:

function addEntryHere($day, $numberId, $numberOfTexts) {
    global $entries;

But realize that, in general, working with global variables is an anti-pattern and is advised against.

Community
  • 1
  • 1
Jeff Lambert
  • 24,395
  • 4
  • 69
  • 96
  • He's not "trying to work with global scope", he expects the array would be passed by reference which can be done by adding a single character to the function's header (`&$entries`). – lafor Nov 06 '15 at 13:21
  • @lafor that's another way to go about it if you don't mind references. I could be misunderstanding them, but OP explicitly states they believe the variable to have global scope yet was confused about why they couldn't reference the variable inside the function. – Jeff Lambert Nov 06 '15 at 13:24
  • thanks @watcher, I followed your advice, but it generated some errors, could you please check the edit to my question? – user3766930 Nov 06 '15 at 13:37
  • 1
    @user3766930 perhaps instead of `$entry->$day` you mean `$entry->day` ? – Jeff Lambert Nov 06 '15 at 13:42
  • Also, I would be wary of how you're looping over `$entries` at the same time as you are modifying `$entries`. Unless there's a compelling reason to do that (which is possible), that could lead to some unexpected edge cases that could lead to some weird results – Jeff Lambert Nov 06 '15 at 13:44
  • Thanks for that hint, I didn't think it through! Do you have any suggestion of how could I change it so that it's a little bit more... safe? – user3766930 Nov 06 '15 at 13:45
  • No, without the entirety of your logic and understanding of what you're trying to do that would be impossible. codereview.stackexchange.com is more appropriate for getting suggestions on how to improve working code – Jeff Lambert Nov 06 '15 at 13:46