0

Question's in the title really - I have this foreach loop and it appears to be iterating over my array twice.

ob_start();

$array = str_split(strtolower($_GET['text']));

foreach ($array as $char) {
    error_log($_GET['text'] . ', ' . sizeof($array) . ', ' . $char);
}

$result = ob_get_contents(); 

I am finding the code above is producing the following log when passing in the URL like so: index.php?text=Hi

[22-Oct-2018 20:05:37 Europe/London] Hi, 2, h
[22-Oct-2018 20:05:37 Europe/London] Hi, 2, i
[22-Oct-2018 20:05:37 Europe/London] Hi, 2, h
[22-Oct-2018 20:05:37 Europe/London] Hi, 2, i

The debug shows the array to only be 2 long, so I'm really not sure what it could be. Thanks!


After more debugging, I have found the following:

if (!isset($_GET['text'])) {
    header('HTTP/1.0 404 Not Found');
    die();
}

echo uniqid() . '</br>';

//ob_start();

$total = 0;
$array = str_split(strtolower($_GET['text']));

foreach ($array as $char) {
    echo $_GET['text'] . ', ' . sizeof($array) . ', ' . $char . '</br>';
}

//$result = ob_get_contents();

echo $result;

Produces this:

5bce311d3d6bd
Hi, 2, h
Hi, 2, i

But un-commenting the two commented out lines, gives me this:

5bce313b9f29d
Hi, 2, h
Hi, 2, i
Hi, 2, h
Hi, 2, i
Toby Smith
  • 1,505
  • 2
  • 15
  • 24
  • 1
    But one loop per character is two iterations - the OP here is showing output with four iterations. Although I cannot see any reason why the code shown would do that. Unless the script ran twice in the same second, writing the log output shown. – Darragh Enright Oct 22 '18 at 19:23
  • 1
    Not reproducible from the sample code alone. Furthermore it's unclear if the code/script gets invoked twice (double request, browser prefetch, etc.), or the output ends up twice in the log. – mario Oct 22 '18 at 19:36
  • @mario I have slightly extended the code sample. The addition appears to be the cause as I also cannot reproduce it without the addition. – Toby Smith Oct 22 '18 at 19:43
  • 1
    I don't see any way that the `ob_XXX` functions could have an effect on `error_log()`. – Barmar Oct 22 '18 at 19:47
  • Try to add an indicator to make sure the script doesn't run twice. For example, you can set a variable outside the loop using `uniqid()` and add this to your log. – HTMHell Oct 22 '18 at 19:48
  • Output buffering can very well [interfere with logging](https://stackoverflow.com/questions/2201841/how-do-i-stop-php-output-buffering-from-eating-error-messages). Still context missing? – mario Oct 22 '18 at 19:51

1 Answers1

0

I think I perhaps have more to learn about the ob_... functionality?

This following code seems to be consistent and reliable:

if (!isset($_GET['text'])) {
    header('HTTP/1.0 404 Not Found');
    die();
}

echo uniqid() . '</br>';

ob_start();

$total = 0;
$array = str_split(strtolower($_GET['text']));

foreach ($array as $char) {
    echo $_GET['text'] . ', ' . sizeof($array) . ', ' . $char . '</br>';
}

ob_flush();
Toby Smith
  • 1,505
  • 2
  • 15
  • 24