使用未定义的常量STDIN - 假设为'STDIN'

时间:2015-05-16 05:30:50

标签: php

<?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

2 个答案:

答案 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);

?>