So I have this really weird issue where I have the following dummy.php script:
<?php
declare(strict_types=1);
$prompt = "Hello there!".
PHP_EOL . "What do you want to know?" .
PHP_EOL . "Proceed by specifying what you want to know:" .
PHP_EOL .": " . PHP_EOL;
readline($prompt);
When I execute the script via php, I get the correct output.
When I place a zsh / bash script in the same directory as the PHP script above, with the contents:
php "./dummy.php";
I get the same output.
When I change the contents of dummy.php to:
<?php
declare(strict_types=1);
$prompt = "Hello there!".
PHP_EOL . "What do you want to know?" .
PHP_EOL . "Proceed by specifying what you want to know:" .
PHP_EOL . "Proceed by specifying what you want to know:" .
PHP_EOL . "Proceed by specifying what you want to know:" .
PHP_EOL .": " . PHP_EOL;
readline($prompt);
I still get the same for both.
When changing it to (added on line of Proceed by specifying what you want to know:):
<?php
declare(strict_types=1);
$prompt = "Hello there!".
PHP_EOL . "What do you want to know?" .
PHP_EOL . "Proceed by specifying what you want to know:" .
PHP_EOL . "Proceed by specifying what you want to know:" .
PHP_EOL . "Proceed by specifying what you want to know:" .
PHP_EOL . "Proceed by specifying what you want to know:" .
PHP_EOL .": " . PHP_EOL;
readline($prompt);
I get the following when executing it via PHP:
Hello there!
What do you want to know?
Proceed by specifying what you want to know:
Proceed by specifying what you want to know:
Proceed by specifying what you want to know:
Proceed by specifying what you want to know:
:
And the following when executing with the shell script:
Hello there!
What do you want to know?
Proceed by specifying what you want to know:
Proceed by specifying what you want to know:
Proceed by specifying what you want to know:
ying what you want to know:
:
What I've tried so far (none of them solved the issue):
- check the
$promptfor unexpected characters like carriage returns, etc. - run the script via
require -rin thezsh. - run the script via
bashinstead ofzsh. - check the readline docs for some maxlength constraints (the docs do not mention any).
If relevant, I am trying this on a mac on the latest software state, where zsh is the default shell, on PHP 8.2.
Why is this happening? It is essential because I am writing multiple interactive CLI automations in PHP using readline, and this issue seems to randomly modify the $prompt provided to readline(), which breaks the whole purpose.
Of course the alternative is to execute all scripts via php in the automations, but doing so via zsh / bash is preferred, as all the other processes of the system are also shell scripts.
echo $prompt; readline()instead ofreadline($prompt);, but I'd still be interested in knowing why this happens.edandvim. The line in a text is edited outside of the flow, and pressing ENTER does actually replace the line. The greatest achievement of a programmer with a lot of time is definitely to make his own text editor, and this is how it starts!