Different result with online PHP compiler and with my own PHP 7.4.1 compiler

Hi there,

I have coded a program for the ANEO sponsored puzzle in PHP. I build the program in VS Code instead of the online editor.
However, when I run the PHP program on my PC with the compiler that came with MAMP, and when I run the program with the online compiler of this website, a different result comes out.

There are 2 changes in the 2 programs, however neither of them matter, because I have standardised the input data, and have the same content in the initial variables on my PC, as I have with the online compiler. The entire rest of the program I have CTRL+C CTRL+V into this site’s compiler.
The 2 changes are:

  • Input data is retreived from a txt file, instead of STDIN
  • The output echo is different due to \n characters used for formatting debug.

I am currently using test1 as my data, where the outputs are the following:

  • MyPC: 50 (correct answer)
  • Online compiler: 49 (wrong answer)

Now I understand that one isn’t supposed to upload any solutions, however I see no other way of debugging this issue, so I am going to upload the program of my PC, and the online program. Plus, my solution is super sloppy and unoptimised, so who cares really… :smiley:

I have linked my GitHub repository with the 2 files and the datafile in question.
GitHub for 2 PHP files

Would love if I could get a fix for this weird bug, or at least some pointers here in the comment section.
Thank you in advance. :slight_smile:

You subtract one from $currentSpeed regardless of whether an answer is found or not.

Having different result locally and in CG is quite strange.
Please note, that CG still uses php 7.3, but none of the changes (I am aware of) can cause such one-off difference.

PS (unrelated to the bug):
You don’t really need to make/use different code for local testing and CG submission. When I test locally, I still read from stdin and redirect the input file to it in commandline, eg. under Windows:
php source.php < test_input.txt > output.txt 2>&1
(Not sure about the Mac syntax.) Also, I print all debug messages with error_log(var_export(…)) and not print_r() so that it does not cause problems during CG submission.

I recommend avoiding relying on php’s automatic type juggling.
For example at $speedLimit = fgets($DataFile);, fgets() also reads the trailing \n, so here you get a string with a number and \n in it. Later you use this as a number, which should be fine in your particular case, but can introduce weird errors otherwise. Why not reading a number with fscanf(), or even using intval()?

1 Like

The problem is, that that is not true. I thought the same, but I know that’s not the case, because on my PC, 50 was the starting value, and I get 50 at the end for currentSpeed. Therefore 1 was not subtracted from my PC version.

As for the using 2 version, setting up the whole redirection is a black box of itself, it really isn’t worth the hassle as to how little gain it gives IMO, plus I have 0 experience with it, so I really don’t know how to do it in the first place :smiley: .
Using print_r instead of error_log(var_export(…)), because print_r takes 3 keystrokes (pr tab), while typing error_log(var_export()) takes an eternity, and if you want to be as maticulate as I am while I program stuff, after 5 of these error log usages, you would want to stab yourself in the face. Plus, print_r is much cleaner, and easier to read.

As for the fgets, that’s a fair point, Im gonna change that in the future :slight_smile:

Oh okay, yeah, the problem was with using fgets, and not fscanf. That \n really did matter. Cuz when I changed it, now my PC version also gives out the same result.

Okay, lesson learnt.
Thanks guys :slight_smile: