<?php
echo "Hello there. So I hear you're learning to be a PHP programmer!\n";
echo "Why don't you type in your name for me:\n";
$name = trim(fgets(STDIN));
echo "\nThanks, " . $name . ", it's really nice to meet you.\n\n";
?>
使用php版本5.5.19 XAMPP
答案 0 :(得分:3)
假设您从命令行界面(CLI)运行:
在文件顶部定义STDIN
常量:
define('STDIN', fopen('php://stdin', 'r'));
或者只需将STDIN
常量替换为:
$name = trim(fgets(fopen('php://stdin', 'r')));
答案 1 :(得分:1)
<?php
echo "Hello there. So I hear you're learning to be a PHP programmer!\n";
echo "Why don't you type in your name for me:\n";
$stdin = fopen('php://stdin', 'r');
$name=trim(fgets($stdin));
echo "\nThanks, " . $name . ", it's really nice to meet you.\n\n";
fclose($file);
?>