Use of undefined constant STDIN - assumed 'STDIN' in C:\wamp\www\study\sayHello.php on line 5
Asked Answered
F

9

10

I want to Learn php & mySQL and I purchased a book (php&mySql: the missing manuals 2edition)

I installed Wampserver2.4 on win8 64bit machine.

Server Configuration

Apache Version : 2.4.4
PHP Version : 5.4.12

in first lesson i got this error :(

Notice: Use of undefined constant STDIN - assumed 'STDIN' in C:\wamp\www\study\sayHello.php on line 5

this is the php code on file "sayHello.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";

?>
Finecut answered 17/1, 2014 at 11:34 Comment(6)
what is STDIN in your script?Bundy
Your code will work fine in the CLIMental
as Rikesh and Abhik mentioned, i added "define('STDIN',fopen("php://stdin","r"));" and the problem was solvedFinecut
The PHP docs mention that only the CLI SAPI defines constants such as STDIN.Tomsk
@Tomsk Could you copy your comment to an answer so we can upvote it?Safety
@Safety sure I can do that. Posted. Thanks!Tomsk
C
35

Just define STDIN constant at top of your file,

define('STDIN',fopen("php://stdin","r"));
Cohune answered 17/1, 2014 at 11:39 Comment(1)
This is a PHP 8 issueLevanter
C
13

when you are trying to run migration form a PHP file using

Artisan::call('migrate');

seems that's time also produce these type of error. For solve this problem you can simple replace your code with

Artisan::call('migrate', ['--force' => true ]);

Make sure you use --force flag if you are in production.

Colwen answered 10/10, 2020 at 8:35 Comment(1)
The exact solution to my problem! Thanks!Lumbricoid
T
9

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 and output_buffering have different default values (more appropriate for the CLI).
  • Does not change the current working directory (CWD) to the script you executed.
Tomsk answered 30/7, 2020 at 16:17 Comment(3)
Note that when running PHP in interactive mode (running php), or with a pipe connected to stdin (running echo Something | php), then STDIN will not be available as a constant. Might not immediately be obvious.Fletafletch
I had to change the define statement to: define('STDIN', fopen('php://input', 'r')); to get this solution to work for me. I'm not sure why, perhaps it depends on the version of PHP that you are using. I'm using PHP version 8.2.3 running on Windows and using Apache httpd 2.4.Gallo
Yep, my answer has a note about php://stdin vs php://input.Tomsk
P
2

Try adding this on the top of your file

define('STDIN',fopen("php://stdin","r"));
Painting answered 17/1, 2014 at 11:39 Comment(0)
D
1

Sometimes you will change the ENV to production and tried to update in that ENV. So better change back that ENV to local. Change .env file.

Earlier: APP_ENV=production

Solution: APP_ENV=local

Deplume answered 4/2, 2021 at 6:31 Comment(4)
Please share more details - why should changing APP_ENV resolve the given problem?Polarity
I got an error while upgrading my database being in production environment. I changed my environment back to local and solve that error.Deplume
That looks like a completely different problem to me. Also, there's a good reason to use different environments - switching them to try out if another setting works looks like a random way of solving the real problemPolarity
This actually solved my problem. Running migration on elasticbeanstalk cause error trying to use the website. I changed to LOCAL and ran migration, afterwards, everything worked fine.Magnetize
D
1

When this error comes from the Youtube Data API code sample, it is because the sample is designed to be run from a CLI and a browser-based implementation does not provide a means of prompting the user for input that is called for by that STDIN line.

Since the auth code is provided as a parameter in the $authUrl variable that gets printed to the screen the first time the script is run, I was able to resolve my issue by replacing

$authCode = trim(fgets(STDIN));

with:

$authCode = $_GET['code'];

Dillman answered 31/3, 2021 at 19:21 Comment(0)
D
1

For Laravel developers encountering this error when running an Artisan command Artisan::call('command');, here's the explanation and solution.

Problem

The error occurs when you execute a command that asks a question, either with $this->confirm(), $this->anticipate(), $this->choice(), etc. The issue arises when you ask a question while executing the command in the code, as it cannot interact and respond to the prompt.

Solution

To solve this problem, you can modify the command to accept an argument or option that provides the answer directly, bypassing the need for user interaction.

For example, when running Artisan::call('db:seed') in a production environment, the command will ask if you really want to run the seeder since it's in a production environment. You need to provide a "yes" or "no" response. By using the --force option, the question will not be asked, and the seeder will continue running. This is just one example; you may have other questions in your command and will need to implement a similar solution.

Dais answered 21/6, 2023 at 15:14 Comment(0)
D
-1

It looks like you are trying to use a constant called STDIN, which does not exist.

STDIN is used to get a currently open stream with fopen.

$file = fopen('file/path');

$name = trim(fgets(STDIN));

Using STDIN without a currently open stream would not work.

I also believe that STDIN only works under cli, but am not 100% sure. If that is the case, use the same code as above, but replace STDIN with $file;

Dubious answered 17/1, 2014 at 11:38 Comment(0)
A
-1

If you want your code to be executed properly with the "STDIN" Constant you have two options:

# php -r "fgets(STDIN);"

or :

# gedit file.php //make a file with <?php fgets(STDIN); ?>
# php file.php // execute the file

The other option :

#php
 <?php
      fgets(STDIN);
 ?>
 //click on ctrl+d

Will not work !! you may have to define the "STDIN" constant inside your code as mentioned above.

Angelangela answered 27/2, 2017 at 18:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.