Use of undefined constant STDIN - assumed 'STDIN' in C:\wamp\www\study\sayHello.php on line 5
Just define STDIN
constant at top of your file,
define('STDIN',fopen("php://stdin","r"));
Explanation
Only the CLI (command line) SAPI defines I/O constants such as STDIN
, STDOUT
, and STDERR
, purely for convenience in that environment.
Solution
As stated in other answers, you can simply define these constants in your PHP code. You can also check defined()
to avoid errors when invoked via CLI. For example:
<?php
if (!defined('STDIN')) {
define('STDIN', fopen('php://stdin', 'r'));
}
However, keep in mind that php://stdin
may not work the way you expect in a non-CLI SAPI, such as Apache or FPM. For example, to access the raw POST body when executed via FPM, you would use php://input
instead.
More Info
PHP has many different SAPI (Server Application Programming Interface), that allow you to execute PHP code in various environments such as your Web server, email server, or the command line (CLI). Examples include:
- CGI
- FPM
- Milter (email filters)
- Apache
- etc
Each SAPI may have slightly different initial conditions and behavior. Some other differences between the CLI SAPI and other SAPIs include:
header()
has no effect.- Some ini settings such as
html_errors
andoutput_buffering
have different default values (more appropriate for the CLI). - Does not change the current working directory (CWD) to the script you executed.