1

I have just upgraded from PHP 7.3.12 to PHP 7.4.0 (released today) on Windows.

This worked until just now:

<?php

    $input = fgets(STDIN);
    var_dump($input);

It now outputs:

bool(false)

It doesn't ask/allow for input at all anymore. It immediately returns a bool false.

I can't find any mention of any recent changes to fgets in either the changelog or the manual page.

What is wrong? What am I supposed to do? Is this a bug? Is it known? Has it been encountered by anyone else?

Also, if this is wrong somehow (in spite of working for so long, and in spite of me finding this code recommended online), what is the "real" way to accept user input/wait for Enter?

I have now downgraded back to 7.3.12 for the moment to fix this issue.

EDIT: Somebody has finally submitted a bug report for this. I sure hope it won't be ignored, as is so often the case in many FOSS projects: https://bugs.php.net/bug.php?id=78883

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Possible duplicate https://stackoverflow.com/questions/6543841/php-cli-getting-input-from-user-and-then-dumping-into-variable-possible – Sam Dean Nov 28 '19 at 16:14
  • Nothing in this world is perfect. You should definitively never be afraid of upgrading your PHP versions in the future -- but two words of advice: Read the changelogs first, and do it in a local environment (so that your production data is not affected). If it all looks good, you upgrade in your production host as well. – Qirel Nov 28 '19 at 16:17
  • @Qirel I did read the changelog, as mentioned. Nothing was said about this. –  Nov 28 '19 at 16:24
  • @u_mulder What do I expect from var_dump? The return value, of course. What's with the random, unprovoked insults from people like you on this site? –  Nov 28 '19 at 16:25
  • For the record, the bug is fixed now, will be part of the 7.4.1 release. – NikiC Dec 04 '19 at 18:14

1 Answers1

1

Confirming that I'm experiencing the same behavior with 7.4. I created a kludgy workaround for now:

    ob_start();                                         // buffer so we don't see the output generated at DOS prompt
    $cmd_line='SET/P phpinput= & SET phpinput';         // Step 1: prompt user to enter a value for variable phpinput & Step 2: display the value for phpinput
    $result=system($cmd_line);                          // Execute
    $result=str_replace('phpinput=', '', $result);      // Clean up the returned result
    ob_end_clean();                                     // resume normal output
    echo "\nReturned result from user typing is: $result\n";
J Kloss
  • 26
  • 3
  • With such a "messy" workaround (no blame on your part, by the way), I'm going to have to stick to 7.3.12 until 7.4.0.1 at least... likely 7.4.1, although that will be a painful wait... –  Nov 28 '19 at 19:10
  • [Bug Report](https://bugs.php.net/bug.php?id=78883) / [PHP 7.4.1 fixed](https://github.com/php/php-src/pull/4961) – Slawa Apr 29 '21 at 09:23