Page 1 of 1

very strange error

Posted: Fri Jun 24, 2005 11:31 pm
by jaymoore_299
I have a recursive function that looks something like this:

<?php
...recursive function... where value in first line is read, then incremented.
$data=file("data.txt");
print "value is: ".$data[0];
?>

The last line is for testing. There is nothing after that line, no php or html code. The very last line in the file is the ?> .
The strange thing is, I get a value, for example, 10, print out for the last line. So supposedly if I go look at the file, I should find a 10 in the first line. But this is not so. I take a look at it and it is 18.

If the value printed is n, then the value that is in the file is n + 8.

I don't understand how this is possible. There is nothing there after the print... except for the ?>. Nothing should be able to change this value in the file. But when I download the file after running this, it's always n + 8.

I saved another php file with just:

<?php
$data=file("data.txt");
print "value is: ".$data[0];
?>

in it, then ran it, and this correctly reads the value as n + 8. This increment isn't supposed to happen. I have no idea where it's coming from. Surprisingly, the function works absolutely perfectly as expected outside of this peculiar increment that seems to come from nowhere.

Posted: Sat Jun 25, 2005 8:49 am
by dethron
create a compressed file that contains your php file and txt file, send us a link to download. i want to try it in my machine.

Posted: Sat Jun 25, 2005 3:38 pm
by Ambush Commander
Use [ php ] tags.

Here's some things you should try:

* Have you turned on error reporting to E_ALL?
* Have you var_dumped all of $data?
* Have you tried accessing the file with other file opening functions (especially ones that read to a string)?

By the way, increment means + 1, so describing a + 8 increase as an increment isn't exactly standard usage.

Posted: Sat Jun 25, 2005 11:52 pm
by jaymoore_299
"* Have you turned on error reporting to E_ALL?"

I tried this and I get no errors.

"* Have you var_dumped all of $data? "

I tried this as well. Nothing unusual. Only the first line has a numeric value, as expected.

"* Have you tried accessing the file with other file opening functions (especially ones that read to a string)?"

I tried using fread. The other function, file_get_contents() is somehow unavailable at my server. This reads the same value for the first line as file() .

There is a pattern to this however. If cycle one has a certain variable with a value of 20, cycle2 with 20, cycle3 with 30. Then the value found in the file would be incorrect by var1 + var2 + var3 = 20 + 20 + 30 = 70.

I have no idea what would cause such a mysterious change in the file. Somehow there is a variable that unknown to me is the sum of the variable values of each recursive cycle, then somehow this is added to the value in the file. Even though the last statement checks the file and gives the expected value, there seems to be some ongoing process that corrupts the file either right after the file is checked, or after the script exits.

Posted: Sat Jun 25, 2005 11:58 pm
by jaymoore_299
I'll try to make a condensed version with the problem. The code is over 200 lines long so it would take me some time. I thought it was a good idea to recursively fill in a template page, however this may be a lesson to stop using php recursively and find other methods.