|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2016-04-13 17:04 UTC] k6dg at outlook dot com
Description:
------------
it only appear in the ZTS version.
use php://stdin get a line with '\n'.
use substr cutoff last char and return it.
use php://stdout print the return value, and print something else.
the return value can not be seen.
Test script:
---------------
function stdin () {
$stdin = fopen('php://stdin','r');
$text = fgets($stdin); // This
fclose($stdin);
return substr($text, 0, -1); // and This couse the problem
}
function stdout(string $data) {
$stdout = fopen('php://stdout','a');
$length = fwrite($stdout, $data);
fclose($stdout);
// $length = file_put_contents ('php://stdout', $data, FILE_APPEND);
return $length;
}
stdout(stdin());
stdout('hello');
Expected result:
----------------
input:
12
output:
hello
Actual result:
--------------
input:
12
output:
12hello
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Nov 19 03:00:02 2025 UTC |
first, I think you confused expected result and actual result. "12hello" should be the expected result while "hello" is printed. In fact, php works as expected. When you type "12<enter> in windows, the string actually is "12\r\n", and result of `substr($test, 0, -1)` is "12\r". "\r" means moving cursor to the begining of the line, and if you continue print something, they will cover the origin display. you can try `stdout('a')` instead of `stdout('hello')` and you will see "a2", because "a" covers "1". If you want to remove the line break in the output, `trim` or `substr($in, 0, -strlen(PHP_EOL))` may be a better choice.